gpt4 book ai didi

linq-to-sql - 将 "is null"转换为 linq to sql 语句

转载 作者:行者123 更新时间:2023-12-04 07:10:09 25 4
gpt4 key购买 nike

我无法将以下 sql 复制为 LINQ 语句

select TableA.* from TableA left outer join TableAinTableB on TableA.Id = TableAId where TableBId is null

以下不返回任何行
from TableA in db.TableA join AinB in db.TableAinTableB on TableA.Id equals TableAId where AinB.TableBId == null select TableA

还尝试了其他一些不起作用的东西。
from TableA in db.TableA join AinB in db.TableAinTableB on TableA.Id equals TableAId where AinB == null select TableA

TableAinTableB 是多对多表。我想要的查询将从 TableA 中提取中间表中没有记录的所有记录。我的 sql 执行我想要的操作,但我不知道如何将其转换为 LINQ to SQL。

我最终通过执行 db.ExecuteQuery("working sql"); 解决了这个问题。但是我想知道在 LINQ 中是否可以进行查询以及如何编写它,或者指向涵盖这种情况的文档的指针。我的搜索没有发现任何我认为有用的东西。

最佳答案

您可以使用 DefaultIfEmpty模拟外连接。
看看这个 sample .

在您的示例中,它类似于:

var q = from a in TableA
join b in TableB on a.Id equals b.Id into g
from b in g.DefaultIfEmpty()
select a;

关于linq-to-sql - 将 "is null"转换为 linq to sql 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/502328/

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