I have Entity Framework entities Events which have an EntityCollection of RSVP. I want to convert the EntityCollection of RSVP to a generic List<> of a POCO class RSVP.
So I want EntityCollection -> List.
What would be the best way to go about achieving this?
So far I have this (it's missing the RSVP part)
var events = from e in _entities.Event.Include("RSVP")
select new BizObjects.Event
{
EventId = e.EventId,
Name = e.Name,
Location = e.Location,
Organizer = e.Organizer,
StartDate = e.StartDate,
EndDate = e.EndDate,
Description = e.Description,
CreatedBy = e.CreatedBy,
CreatedOn = e.CreatedOn,
ModifiedBy = e.ModifiedBy,
ModifiedOn = e.ModifiedOn,
RSVPs = ???
};
Thanks.
I suggest you put the "select" code into extension method named something like "ToPoco(this Event event)" (you will use this for single "Event" conversion).
You must also implement another extension method for multiple "Event" conversion like List<BizObjects.Event> ToPoco(this List<Event> events)
extension, which just call the BizObjects.Event Poco(this Event event)
in a loop.
After that your query will look like this:
var events = (from e in _entities.Event.Include("RSVP")).ToList().ToPoco();
About RSVPs:
You just normally create another extension method for RSVP conversion like
List<BizObjects.RSVP> ToPoco(this List<RSVP> RSVPs)
Then you can then call RSVPs = e.RSVPs.ToList().ToPoco()
Solution for directly fit to you code could be also something like that:
RSVPs = e.RSVPs.Select(rsvp => new RSVP { //do mapping })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With