我试图使用实体框架的结果初始化列表。下面是错误:
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>();
}
}
你需要首先执行并返回一个IEnumerable,然后使用linq to Objects创建一个列表
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();