【问题标题】:Include and ThenInclude with multiple levels of collections [duplicate]Include 和 ThenInclude 与多个级别的集合[重复]
【发布时间】:2020-01-22 00:21:58
【问题描述】:

我正在尝试查询具有多个集合级别的实体,以及单个级别的多个集合。我正在使用Include()ThenInclude(),但没有取得多大成功。我发现的示例在同一级别上没有多个集合,并且我没有任何运气将该技术应用于我的用例。

这是我的实体的简化图。带有 [] 的是集合:

Home
   Areas[]
       Area
           Name
           Categories[]
               Name
               Recommendations[]
               Subcategories[]
                   Name
                   Recommendations[]
       Area
           Name
           Categories[]
               Name
               Recommendations[]
               Subcategories[]
                   Name
                   Recommendations[]   

我已经走到这一步了:

result = Home
    .Include(x => x.Areas)
    .ThenInclude(a => a.Categories)
    .ThenInclude(c => c.Subcategories)
    .ThenInclude(s => s.Recommendations)

但是,这错过了Categories[].Recommendations[] 集合。这是因为同一级别有两个集合(Recommendations[]Subcategories[])。

对构建此查询的方法有什么建议,以便我可以得到所需的结果?

谢谢。

【问题讨论】:

    标签: entity-framework-core entity-framework-core-2.1


    【解决方案1】:

    您必须为每个级别致电Include

    result = Home
        .Include(x => x.Areas)
            .ThenInclude(a => a.Categories)
            .ThenInclude(c => c.Subcategories)
            .ThenInclude(s => s.Recommendations)
        .Include(x => x.Areas)
            .ThenInclude(a => a.Recommendations)
    

    【讨论】:

      【解决方案2】:

      您必须按如下方式编写查询:

      result = Home
          .Include(x => x.Areas)
              .ThenInclude(a => a.Categories)
              .ThenInclude(s => s.Recommendations)
         .Include(x => x.Areas)
             .ThenInclude(c => c.Subcategories)
      

      【讨论】:

        猜你喜欢
        • 2019-01-03
        • 1970-01-01
        • 2018-09-15
        • 2020-01-25
        • 1970-01-01
        • 1970-01-01
        • 2020-08-24
        • 1970-01-01
        • 2020-02-12
        相关资源
        最近更新 更多