Jaymz
Jaymz

Reputation: 6348

LINQ to SQL complex join with mixed conditions

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 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?

Upvotes: 3

Views: 2040

Answers (1)

George Duckett
George Duckett

Reputation: 32428

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.

Upvotes: 2

Related Questions