Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

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.

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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...