2011-04-05 67 views
2

我试图使用实体框架的结果初始化列表。这是错误:初始化列表<T>使用具有外键的EntityCollection

LINQ to Entities无法识别方法'System.Collections.Generic.List 1[Domain.Entities.Person] ToList[Person](System.Collections.Generic.IEnumerable 1 [Domain.Entities.Person])''方法,并且此方法无法转换为存储表达式。

public List<Domain.Entities.Event> Events 
     { 
      get 
      { 
       Entities context = new Entities(connectionString); 

       return (from c in context.Events.Include("EventPeople") 
         select new Domain.Entities.Event() 
         { 
          ID = c.ID, 
          Title = c.Title, 
          Description = c.Description, 
          Date = c.Date, 
          People = (from ep in c.EventPeople 
            select new Domain.Entities.Person() 
            { 
             ID = ep.ID, 
             Name = ep.Name 
            }).ToList<Person>() 
         }).ToList<Domain.Entities.Event>(); 
      } 
     } 

回答

3

您需要执行第一,并返回一个IEnumerable然后用LINQ到对象创建一个列表

var events = (from c in context.Events.Include("EventPeople") 
        select new 
        { 
         ID = c.ID, 
         Title = c.Title, 
         Description = c.Description, 
         Date = c.Date, 
         People = (from ep in c.EventPeople 
           select new Domain.Entities.Person() 
           { 
            ID = ep.ID, 
            Name = ep.Name 
           }) 
        }).ToList(); 
return events.Select(e => new Domain.Entities.Event() 
        { 
         ID = e.ID, 
         Title = e.Title, 
         Description = e.Description, 
         Date = e.Date, 
         People = e.People.ToList() 
        }).ToList();