gpt4 book ai didi

linq - 使用具有外键的 EntityCollection 初始化 List

转载 作者:行者123 更新时间:2023-12-04 06:29:51 26 4
gpt4 key购买 nike

我正在尝试使用 Entity Framework 的结果初始化 List。这是错误:

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

 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();

关于linq - 使用具有外键的 EntityCollection 初始化 List<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5556523/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com