如何将EntityCollection <t>转换为List <pocoobj> </pocoobj> </t>

时间:2010-03-13 07:44:43

标签: c# asp.net asp.net-mvc entity-framework

我有Entity Framework实体的事件,其EntityCollection为RSVP。我想将RSVP的EntityCollection转换为通用List&lt;&gt;一个POCO类RSVP。

所以我想要EntityCollection - &gt;列表。

实现这一目标的最佳方式是什么?

到目前为止,我有这个(它缺少RSVP部分)

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 = ???
                     };

感谢。

2 个答案:

答案 0 :(得分:1)

我建议你将“选择”代码放入名为“ToPoco(此事件事件)”的扩展方法中(您将使用此代码进行单个“事件”转换)。

您还必须为多个“事件”转换实施其他扩展方法,例如List<BizObjects.Event> ToPoco(this List<Event> events)扩展,只需在循环中调用BizObjects.Event Poco(this Event event)

之后,您的查询将如下所示:

var events = (from e in _entities.Event.Include("RSVP")).ToList().ToPoco();

关于RSVP:

您通常只为

创建另一种RSVP转换扩展方法

List<BizObjects.RSVP> ToPoco(this List<RSVP> RSVPs)

然后,您可以拨打RSVPs = e.RSVPs.ToList().ToPoco()


直接适合您的代码的解决方案也可能是这样的:

RSVPs = e.RSVPs.Select(rsvp => new RSVP  { //do mapping })

答案 1 :(得分:0)

    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.
                             }
                 };