Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an EntityCollection<T> to List<POCOObj>

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.

like image 847
Guillermo Gomez Avatar asked Nov 14 '22 12:11

Guillermo Gomez


1 Answers

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 })
like image 102
Peter Stegnar Avatar answered Dec 18 '22 22:12

Peter Stegnar