开发者

LINQ to SQL complex join with mixed conditions

开发者 https://www.devze.com 2023-03-15 11:32 出处:网络
I have a SQL statement which I am trying to convert into LINQ to SQL, and I\'ve managed to get most of it converted, but have come across one statement which I can\'t wrap my head around in LINQ.

I have a SQL statement which I am trying to convert into LINQ to SQL, and I've managed to get most of it converted, but have come across one statement which I can't wrap my head around in LINQ.

The section of the SQL query that's causing the headache is:

SELECT *
FROM step
INNER JOIN action on 
    (step.NextAction = action.ID and step.ActionStatus != 4) or 
    (step.ACTION = action.ID and step.ActionStatus = 4)

step is a table containing a sequence of 开发者_StackOverflow中文版actions, action is the list of actions available. ActionStatus is an index into a list of statuses - 4 == 'Failed'.

Basically, for actions which are not Failed, it needs to return the next action. If the action has Failed, it returns the current action.

This is just one of the joins (there are a total of 10 tables in the full query), most of them are straight forward equijoins, some on multiple conditions, but I've been able to write them in LINQ with no issue. This one though, I can't see how it would be written.

I saw this answer, but also can't see how to apply that in this scenario. Any ideas?


from s in step
from a in action
where (s.NextAction = a.ID && s.ActionStatus != 4) || (s.Action = a.ID && s.ActionStatus = 4)
select new { Step = s, Action = a };

You might want to look at the SQL generated and optimise if needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消