Linq的大于条件加盟 [英] Greater Than Condition in Linq Join
问题描述
我试图有条件地连接两个表,但这给了我语法错误.我试图在网络中找到解决方案,但我找不到如何用条件进行条件联接.唯一的另一种选择是首先从一个表中获取值,然后再次进行查询.
I had tried to join two table conditionally but it is giving me syntax error. I tried to find solution in the net but i cannot find how to do conditional join with condition. The only other alternative is to get the value first from one table and make a query again.
我只想确认是否还有其他方法可以使用linq进行条件连接.
I just want to confirm if there is any other way to do conditional join with linq.
这是我的代码,我试图找到所有等于或低于我的位置.基本上,我想得到我的同辈和下属.
Here is my code, I am trying to find all position that is equal or lower than me. Basically I want to get my peers and subordinates.
from e in entity.M_Employee
join p in entity.M_Position on e.PostionId >= p.PositionId
select p;
推荐答案
使用LINQ连接无法做到这一点-LINQ仅支持 equijoins .但是,您可以这样做:
You can't do that with a LINQ joins - LINQ only supports equijoins. However, you can do this:
var query = from e in entity.M_Employee
from p in entity.M_Position
where e.PostionId >= p.PositionId
select p;
或者稍微替代但等效的方法:
Or a slightly alternative but equivalent approach:
var query = entity.M_Employee
.SelectMany(e => entity.M_Position
.Where(p => e.PostionId >= p.PositionId));
这篇关于Linq的大于条件加盟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!