【问题标题】:How can I use LINQ to achieve the different joins SQL has to offer?如何使用 LINQ 实现 SQL 必须提供的不同连接?
【发布时间】:2013-07-24 13:02:00
【问题描述】:

我需要转换这个查询:

SELECT *
FROM Table1 AS t1
left join Table2 t2 
    on t1.Column1 = t2.Column1
left join Table2 t2
    on t1.Column2 = t2.Column2 and t2.Column3 = 1

到 LinqToSQL 和 Lambda。

我一直在寻找对 LINQ 中 SQL 连接的良好解释,但没有找到任何清楚说明如何利用 LINQ 来实现 SQL 中提供的不同连接的东西。

谁能解释一下如何在 SQL 中利用不同的 SQL 连接。

【问题讨论】:

标签: c# linq-to-sql lambda


【解决方案1】:

试试这样的:

AlternativeLearningPlanYears
.GroupJoin
(
    Leas,
    x => x.DistrictLeaId,
    y => y.LeaId,
    (x,y) => new {x,y}
)
.GroupJoin
(
    Leas,
    x => new {x.PrivateInstitutionId,1},
    y => new {y.PrivateInstitutionId,IsPrivateInstitution},
    (x,y) => new {x,y}
)
.Where
(
    z => z.x.SchoolYear == 23
    && z.x.Granted == 1
    && z.x.PrivateInstitutionId == null
    && z.x.DistrictName == null
)

我没有你的架构,所以可能有一些错误,所以发布它们,我们会解决它们..

【讨论】:

    猜你喜欢
    • 2014-09-25
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 2014-06-07
    • 2014-07-26
    • 2016-12-04
    相关资源
    最近更新 更多