【问题标题】:EF6 Single LINQ Query for nested Lists嵌套列表的 EF6 单 LINQ 查询
【发布时间】:2017-01-12 21:51:41
【问题描述】:

我想使用 EntityFramework 仅使用单个 LINQ 查询来填充 DB 实体中的嵌套列表。

我有 3 个表实体。 第一类Cities 包含List<Houses>Houses 包含List<Residents>

那些课程:

class Cities
{
    long CityId {get;set;} 
    string Name {get;set;} 
    List<House> Houses {get;set;} 

}

class Houses 
{
    long CityId {get;set;} 
    string Address {get;set;} 
    List<Resident> Residents {get;set;}

}

class Residents 
{
   long HouseId {get;set;} 
   string FirstName {get;set;} 
   string LastName {get;set;} 
}

我想要实现的是这样的:

var cities = ( from city in db.Cities
               select new  // Creating anonymous type to fill List of Houses 
               {
                  CityId = city.CityId,
                  Name   = city.Name, 
                  Houses = db.Houses.Where(h=>h.CityId == city.CityId)
                                    .Select( new // Another anonymous type, but this time this isn't working
                                    {
                                        HouseId = h.HouseId,
                                        Address = h.Address,
                                        Residents = db.Residents.Where(r=>r.HouseId == h.HouseId).ToList()
                                    }).ToList()
                                    .Select( h => new Houses
                                    {
                                        HouseId = h.HouseId,
                                        Address = h.Address,
                                        Residents = h.Houses
                                    }).ToList()
               })
               .ToList()
               .Select( c=> new Cities
               {
                  CityId = c.CityId
                  Name   = c.Name, 
                  Houses = c.Houses
               }).ToList()

不幸的是,我收到了错误 The entity or complex type Houses cannot be constructed in a LINQ to Entities

它适用于Houses = db.Houses.Where(h=&gt;h.CityId ==city.CityId).ToList()。 但是我在Houses中失去了Residents

甚至可以使用一个 LINQ 查询吗?

【问题讨论】:

  • 您是否尝试使用 LINQ 查询写入数据库?
  • Residents = h.Houses 一定是错字。此外,强烈建议使用单数类名。

标签: c# entity-framework linq


【解决方案1】:

您只需要在您的城市查询中包含房屋和居民:

var cities = db.Cities.Include(c => c.Houses.Select(h => h.Residents)).ToList();

【讨论】:

  • 如果可以返回实体类型,这实际上是最好的。如果选择匿名类型是在Include 之后和ToList 之前完成的,则不会起作用。 edit 刚刚意识到实体对象是从匿名类型重建的,所以我的评论有点多余:)
  • @Sergey_Berezovskiy:谢谢。我在正确创建导航属性时遇到了一些小问题,但在那之后,一切都很好!
【解决方案2】:

您应该使用导航属性,而不是单独的db 访问权限

var Cities = (from city in db.Cities
           select new  // Creating anonymous type to fill List of Houses 
           {
              CityId = city.CityId,
              Name   = city.Name, 
              Houses = city.Houses.Select( new
                                {
                                    HouseId = h.HouseId,
                                    Address = h.Address,
                                    Residents = h.Residents.ToList()
                                }).ToList()
                                .Select( h => new Houses
                                {
                                    HouseId = h.HouseId,
                                    Address = h.Address,
                                    Residents = h.Houses
                                }).ToList()
           })
           .ToList()
           .Select( c=> new Cities
           {
              CityId = c.CityId
              Name   = c.Name, 
              Houses = c.Houses
           }).ToList()

没有检查整个语法,只将db.Houses.Where(...)替换为city.Houses(与Residents相同),因此可能存在一些其他问题。

【讨论】:

    猜你喜欢
    • 2021-06-12
    • 1970-01-01
    • 2023-04-09
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多