LINQ to Entities Join on Nullable Field where Null Implies "Match All"

You can modify your code like:

int taskId = 2;

query = from a in Table_A
        where a.StatusCode != "DONE"
           && a.Inbound
        join b in Table_B
            on a.Id equals b.Id_Table_A
        from c in Table_C
        where 2 == c.Id_Task
           && b.DataType == c.DataType
           && (c.State == null || a.State.Equals(c.State))
        select a.Id;

A join is essentially a where clause, so here we can use the where clause due to the restrictions with join.


I managed to get this to work by moving the "DataType" check from the WHERE to the JOIN, and moving the "State" check from the JOIN to the WHERE. The resulting code that worked as I expected is as follows:

query = from a in Table_A
        where a.StatusCode != "DONE"
           && a.Inbound
        join b in Table_B
            on a.Id equals b.Id_Table_A
        join c in Table_C
            on b.DataType equals c.DataType
        where 2 == c.Id_Task
            && (c.State ?? a.State) == a.State
        select a.Id;

Many thanks to everybody who has taken a look at this for me. :)


You can use "from" syntax instead of "join"

from a in TableA
from b in TableB
.Where(x => (x.Buy ?? a.Buy) == a.Buy
        && (x.Parity ?? a.Parity) == a.Parity)