Linq Join 中的大于条件 [英] Greater Than Condition in Linq Join

查看:28
本文介绍了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 Join 中的大于条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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