【问题标题】:Linq Lambda multiple tables ( 4 tables ) LEFT JOINLinq Lambda 多表(4表)LEFT JOIN
【发布时间】:2015-02-07 11:40:41
【问题描述】:

我有 4 张桌子;

TMain >> MainId (PK)

T1 >> T1_Id, MainId, X (PK and FK) X is decimal

T2 >> T2_Id, MainId, X (PK and FK) X is decimal

T3 >> T3_Id, MainId, X (PK and FK) X is decimal

这里是SQL输出;

SELECT TMain.*, (ISNULL(T1.X,0) + ISNULL(T2.X,0) + ISNULL(T3.X,0)) AS TOTAL FROM TMain

LEFT OUTER JOIN T1 ON TMain.MainId = T1.MainId

LEFT OUTER JOIN T2 ON TMain.MainId = T2.MainId

LEFT OUTER JOIN T3 ON TMain.MainId = T3.MainId

我该怎么写 LINQ LAMDA

   var AbbA = MyContext.TMain
                    .GroupJoin(
                        MyContext.T1,
                        q1 => q1.TMainId,
                        q2 => q2.TMainId,
                        (x, y) => new { A = x, T1_A = y })
                            .SelectMany(
                            xy => xy.T1_A.DefaultIfEmpty(),
                            (x, y) => new { A = x.A, T1_A = y })
                    .GroupJoin(
                        MyContext.T2,
                        q1 => q1.A.TMainId,
                        q2 => q2.TMainId,
                        (x, y) => new { A = x, T2_A = y })
                            .SelectMany(
                            xy => xy.T2_A.DefaultIfEmpty(),
                            (x, y) => new { A = x.A, T2_A = y })
                    .GroupJoin(
                        MyContext.T3,
                        q1 => q1.A.A.TMainId,
                        q2 => q2.TMainId,
                        (x, y) => new { A = x, T3_A = y })
                            .SelectMany(
                            xy => xy.T3_A.DefaultIfEmpty(),
                            (x, y) => new { A = x.A, T3_A = y })
                    .Select(q => new 
                    {
                        TMainId = q.A.A.A.TMainId,
                        Total = (q.T3_A.X == null ? 0 : q.T3_A.X) +
                                (q.A.T2_A.X == null ? 0 : q.A.T2_A.X) +
                                (q.A.A.T1_A.X == null ? 0 : q.A.A.T1_A.X),
                    }).ToList();

所以我想访问 T1 字段或 TMain 字段

我写了 q.A.A.T1_A.X 或 q.A.A.A.在 linq 中选择

这是真的吗?还是有最简单的方法?

【问题讨论】:

标签: linq entity-framework lambda


【解决方案1】:

我现在无法测试是否可行,但有时您可以编写 GroupJoin (如果您希望 T1 有 0 或 N 个寄存器):

.GroupJoin(MyContext.T1,
     q1 => q1.TMainId,
     q2 => q2.TMainId,
     (x, y) => new { A = x, T1_A = y.DefaultIfEmpty() })

或(如果您希望 T1 有 0 或 1 个寄存器)

.GroupJoin(MyContext.T1,
     q1 => q1.TMainId,
     q2 => q2.TMainId,
     (x, y) => new { A = x, T1_A = y.FirstOrDefault() })

这总是取决于你想为下一个 lambda 返回什么,在这种情况下你不需要 SelectMany()。但是,如果您无法相应地映射表之间的关系,则此代码将是您可以使用 linq/lambda 获得的最简单的左连接。

如果用“HasOptional”映射表TMain和T1之间的关系可以简化,比如:

modelBuilder.Entity<T1>()
            .HasOptional(x => x.TMain)
            .WithMany(y => y.T1s)
            .HasForeignKey(x => x.TMaidId);

“HasOptional()”向实体框架显示此关系是可选的,因此将使用 LEFT JOIN 来挂载查询。如果使用“HasRequired()”,将使用 JOIN。

所以,你可以使用 Include():

var AbbA = MyContext.TMain
     .Include(x => x.T1s)

向左连接 T1 和 T2:

var AbbA = MyContext.TMain
     .Include(x => x.T1s.Select(y => y.T2s))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 2012-06-21
    相关资源
    最近更新 更多