LINQ的 - 左加入多(OR)条件 [英] Linq - left join on multiple (OR) conditions

查看:134
本文介绍了LINQ的 - 左加入多(OR)条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做一个左连接上那里的条件OR值,而不是AND将多个条件。我发现很多后者的样本,但我在努力找到正确答案为我的方案。

 从TableA中
在新表B加入B {a.col1,a.col2}等于新的{b.col1,b.col2}
通过为G组
选择新的(){COL1 = a.col1,COL2 = a.col2,计数= g.Count()}

的伟大工程的连接,所有条件都必须匹配。我需要让加盟匹配a.col1 = b.col1 OR = a.col2 b.col2。

我知道这一定是容易的,但我已经来了这个空白!

编辑:

要多加一点信息,查询的目的是为了获得一个包含所有从'一'加的匹配记录在'B'计数领域的投影。我已经修改上面的例子,试图说明我后。当我在上面运行使用方法乔恩斯基特注意到我得到的来自所有记录的计数,不计数的B中的相关记录。

基本左连接工作正常:

 从TableA中
从B表B中
。凡(B = GT;(a.col1 == || b.col1 == a.col2 b.col2))
.DefaultIfEmpty()
选择新{COL1 = a.col1,COL2 = a.col2}

如果我修改它来分组添加如下

 从TableA中
从B表B中
。凡(B = GT;(a.col1 == || b.col1 == a.col2 b.col2))
.DefaultIfEmpty()
通过a.col1为G组
选择新{COL1 = g.Key,计数= g.Count()}

我得到的记录的计数从回来了 - 记录不计数在B匹配

编辑:

我给的答案乔恩 - 我已经解决了我的问题计数 - 我没有意识到我可以使用LAMDA过滤计数(g.Count(X => X = NULL)!)。另外,我需要B组通过,而不是由一个像我上面了。这给出正确的结果,但SQL是不是有效,因为我的手,因为它增加了一个correalated子查询写 - 如果任何人都可以更好的办法建议写它来模拟下面的SQL我最好的AP preciate吧!

 选择a.col1,计数(b.col1)
从tableA的
左连接表B b
在a.col1 = b.col1
或a.col2 = b.col2
通过a.col1组


解决方案

LINQ只能直接支持等值连接。如果你想要做的任何其他类型的加入,你基本上需要一个交叉联接和,其中

 从TableA中
从B表B中
其中,a.col1 == || b.col1 a.col2 == b.col2
选择 ...

这也许值得一试的查询计划是生成的SQL看起来像什么样。可能有这样做的更有效的方式,但是这可能是的简单的办法。

I need to do a left join on multiple conditions where the conditions are ORs rather than ANDs. I've found lots of samples of the latter but am struggling to get the right answer for my scenario.

from a in tablea
join b in tableb on new { a.col1, a.col2 } equals new { b.col1, b.col2 }
group a by a into g
select new () { col1 = a.col1, col2 = a.col2, count = g.Count() }

works great for joins where all conditions must match. I need to get the join to match on a.col1 = b.col1 OR a.col2 = b.col2.

I know it must be easy but I've coming up blank on this!

Edit:

To give a little more info, the purpose of the query is to get a projection containing all of the fields from 'a' plus a count of the matching records in 'b'. I've amended the sample above to try and illustrate what I'm after. When I run with the above using the approach Jon Skeet has noted I'm getting a count of all records from a, not the count of the related records in b.

The basic left join works fine:

from a in tablea
from b in tableb
.Where( b => ( a.col1 == b.col1 || a.col2 == b.col2))
.DefaultIfEmpty()
select new { col1 = a.col1, col2 = a.col2 }

If I revise it to add the grouping as below

from a in tablea
from b in tableb
.Where( b => ( a.col1 == b.col1 || a.col2 == b.col2))
.DefaultIfEmpty()
group a by a.col1 into g
select new { col1 = g.Key, count = g.Count() }

I'm getting the count of the records returned from a - not the count of records in matching in b.

Edit:

I'll give the answer to Jon - I've solved my count issue - I hadn't realised I could use a lamda to filter the count (g.Count(x => x != null)). Plus I need to group b by a rather than a by a as I had above. This gives the correct result but the SQL is not as efficient as I'd write it by hand as it adds a correalated sub query - if anyone can advise a better way of writing it to simulate the following SQL I'd appreciate it!

select a.col1, count(b.col1)
from tablea a
left join tableb b
on a.col1 = b.col1
or a.col2 = b.col2
group by a.col1

解决方案

LINQ only directly supports equijoins. If you want to do any other kind of join, you basically need a cross-join and where:

from a in tablea
from b in tableb
where a.col1 == b.col1 || a.col2 == b.col2
select ...

It's probably worth checking what the generated SQL looks like and what the query plan is like. There may be more efficient ways of doing it, but this is probably the simplest approach.

这篇关于LINQ的 - 左加入多(OR)条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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