I need an efficient LINQ query (in method syntax if possible) to get all items from a collection A that have no corresponding key in a second collection B (1 to n) or if there exist elements in B, take only those that have MyValue null. In short: return all elements of A that do not exist in B or if they exist in B where at minimum one row has MyValue = null.
table A
{
int MyKey (primary_key);
}
table B
{
int MyKey (foreign_key to A.MyKey);
string MyValue;
}
I was trying Except(), but this only works, when both collections are of the same type. I was trying GroupJoin(), but I did not find a way how to remove the duplicates after joining.
a.GroupJoin(
b.Where(item => item.Value = null),
el => el.MyKey,
el2 => el2.MyKey,
(el3, el4) => el3);
With this, I filter out items in B, that are after joined again because they don't exist anymore.
In pure sql it is very easy to achive:
select * from A a left join B b on a.MyKey = b.MyKey where MyValue is null;