【问题标题】:Include Nested Tables in LINQ Method syntax在 LINQ 方法语法中包含嵌套表
【发布时间】:2016-01-14 05:23:02
【问题描述】:

我有 4 个用外键连接的表。

MasterTable01(外键MasterTable02Id

MasterTable02(外键MasterTable03Id

MasterTable03(外键MasterTable04Id

MasterTable04

现在我想在我的结果集中包含所有这些表。

var result = context.MasterTable01
                    .Include(MasterTable01)
                    .Include(MasterTable02.MasterTable03)
                    .Include(MasterTable03.MasterTable04)

以及如何读取嵌套值。

result.foreach(x => new myObject(x.MasterTable01.Desc,
                                 x.MasterTable01.MasterTable02.Desc,
                                 x.MasterTable01.MasterTable02.MasterTable03.Desc));

【问题讨论】:

  • 这段代码不会编译。

标签: c# entity-framework linq extension-methods


【解决方案1】:

如果表格是一对多,那么:

var result=context.MasterTable01
  .Include(t1=>t1.MasterTable02.Select(t2=>t2.MasterTable03.Select(t3=>t3.MasterTable04))

如果表格是 1:1 那么:

var result=context.MasterTable01
  .Include(t1=>t1.MasterTable02.MasterTable03.MasterTable04)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2022-01-27
    相关资源
    最近更新 更多