Linq组加入和where连接表的属性声明 [英] Linq group join and where statement on property of the joined table

查看:93
本文介绍了Linq组加入和where连接表的属性声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我相信我已经发现, group join 是一个左外连接,这就是我需要的。但是,我需要检查连接的表属性是否为 null 。但是我还没有工作。

So I believe I have found out that group join is a left outer join and that is what I need. But I need to check if the joined tables property is null. But I haven't got it working yet.

所以我基本上需要在Linq实体框架中相当于这个查询

So I basically need the equivalent of this query in Linq Entity Framework

SELECT
    id, test, test2 
FROM Table1 
LEFT OUTER JOIN Table2 ON 
    table1.id = table2.id 
WHERE table2.example = NULL;

我已经尝试用lambda这样做,但没有任何成功。我似乎无法获取 table2 属性示例的其中语句。

I have tried to do this with lambda but without any success yet. I can't seem to get the hold of the table2 property example for the where statement.

推荐答案

您可以使用LINQ扩展方法(GroupJoin)来流览此示例:

You can flow this example using LINQ Extension Method (GroupJoin):

    Table1.GroupJoin(Table2,
                    x => x.ID,
                    y => y.ID,
                    (tbl1, tbl2) => new {Table1=tbl1, Table2 =tbl2.DefaultIfEmpty()})
                    .SelectMany(
                    tbl => tbl.Table2.Where(t2 => t2.example == null).Select(x => new
                    {
                        id= tbl.Table1.ID,
                        test = tbl.Table1.Test,
                        test2 = tbl.Table2.Test
                    }))ToList();

这篇关于Linq组加入和where连接表的属性声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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