views:

503

answers:

3

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.

A: 

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 })
Peter Stegnar
I tried your last solution, but had to add .ToList to .Select. However, I got an EntityFramework error stating it couldn't convert ToList(). I will post the error later when I get a chance.You have pointed me in the right direction. Thanks!
ggomez
+1  A: 

Checkout this Projections and Includes blog post.

kervin
Thanks, I will certainly check this out!
ggomez
A: 
    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 = from r in e.RSVP
                             select new BizObjects.RSVP
                             {
                                 RSVPId = RSVPId,
                                 // etc.
                             }
                 };
Craig Stuntz