Help with Implicit Explicit operators
This is my operator definition
I've not been able to find the exacts. With this definition, will it convert from Entry to ViewModel or ViewModel to Entry? Because, with the said implementation, following conversion doesn't work:
EventData = Context.Entries.Where(e => e.UserId == UserId);
The error is Can't convert from IQueryable<Entry> to IQueryable<ViewModel>9 Replies
It converts an
Entry
to a EventViewModel
, but that doesn't mean you'll get an automatic conversion from an IQueryable<Entry>
to an IQueryable<EventViewModel>
🙁
So what's the way to do that
Particularly as this is going up to a database, and the database has no idea how to run your user-defined conversion
.Select(x => (EventViewModel)x)
? You need something to convert each element to an EventViewModel
Right I used Select but wanted to automate it a bit
Although at that point you might as well drop the operator, as it really is code smell, and just do
.Select(x => new EventViewModel(x))
.Select(x => new EventViewModel(x))
that seems like a constructor, so I'll have to implement a constructor for ViewModel, right?Yeah, that'll be neater than an implicit conversion
In my projects, we make an extension method
.ToDto()
or sorts and then do
To map from entities to DTOsPerfect, thanks a bunch you guys 🙂