gpt4 book ai didi

c# - 使用 lambda 表达式左连接

转载 作者:太空狗 更新时间:2023-10-30 01:09:28 25 4
gpt4 key购买 nike

我有表A和表B。
我想使用 lambda 表达式执行左连接。这是相等的sql语句:

SELECT *
FROM tableA A
LEFT JOIN tableB B ON A.userId=B.userId

我如何使用 lambda 表达式来做到这一点?

最佳答案

It's usually an error to use an explicit join in LINQ to Entities.

相反,使用导航属性:

var q = Context.TableAs.Select(a => new { a.Foo, a.TableB.Bar });

LINQ to Entities 将合并空引用。因此,如果对于 TableAs 中的某些记录,a.TableBnull,则 a.TableB.Bar 将返回 null 而不是给你一个空引用异常。所以它的行为类似于 SQL LEFT JOIN

关于c# - 使用 lambda 表达式左连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6665716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com