初始化列表使用具有外键的 EntityCollection

发布于 2024-10-30 04:41:29 字数 1199 浏览 0 评论 0原文

我正在尝试使用实体框架的结果初始化列表。这是错误:

LINQ to Entities 无法识别方法 'System.Collections.Generic.List1[Domain.Entities.Person] ToList[Person](System.Collections.Generic.IEnumerable1[ 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>();
            }
        }

I'm trying to initialize a List using results from the entity framework. Here is the error:

LINQ to Entities does not recognize the method 'System.Collections.Generic.List1[Domain.Entities.Person] ToList[Person](System.Collections.Generic.IEnumerable1[Domain.Entities.Person])' method, and this method cannot be translated into a store expression.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城缀染半城烟沙 2024-11-06 04:41:29

您需要首先执行并返回 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();

You need to execute first and return an IEnumerable then with linq to Objects create a list

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文