转换“为空"到linq to sql语句 [英] Converting "is null" into a linq to sql statement

查看:45
本文介绍了转换“为空"到linq to sql语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having trouble replicating the following sql as a LINQ statement

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

还尝试了其他一些无效的方法.

Also tried and a few other things that didn't work.

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.

TableAinTableB is a many to many table. The query I want will pull all the records from TableA that have no records in the middle table. My sql does what I want but I have no idea how to convert it to LINQ to SQL.

我最终只是通过执行db.ExecuteQuery("working sql");而解决了该问题.但是我想知道在LINQ中是否可以进行查询以及如何编写查询,或者是涉及此情况的文档的指针.我的搜索未发现我发现有用的任何内容.

I ended up working around it by just doing a db.ExecuteQuery("working sql"); But I would like to know if the query is possible in LINQ and how to write it, or a pointer to a document that covers this scenario. My searching did not uncover anything I found useful.

推荐答案

您可以使用示例.

You can use the DefaultIfEmpty to simulate an outer join. Check out this sample.

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

In your example it's something like:

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语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆