I've inherited a database that wasn't designed exactly optimally, and I need to manipulate some data. Let me give a more common analogy of the kind of thing I have to do:
Let's say we have a Student
table, a StudentClass
table keeping record of all the classes he attended, and a StudentTeacher
table that stores all the teachers who taught this student. Yes, I know it's a dumb design and it would make more sense to store the teacher on the Class table - but that's what we're working with.
I now want to clean up the data, and I want to find all the places where a student has a teacher but no classes, or a class but no teachers. SQL thus:
select *
from StudentClass sc
full outer join StudentTeacher st on st.StudentID = sc.StudentID
where st.id is null or sc.id is null
How do you do that in Linq?
for the given 2 collections a and b, a required full outer join might be as following:
If a and b are not of the same type, then 2 separate left outer joins are required:
here is a one line option using Concat():
A start...
See also http://www.linqpad.net/ for more samples Good tool to play with
Extension method:
Test:
I think I have the answer here, which is not as elegant as I'd hoped, but it should do the trick:
You could probably squeeze these two statements into one, but I think you'd sacrifice code clarity.
Based on Shaul's answer, but with a little streamlining:
Note that for a full outer join, this can work, too. Leave out the
where
clause and use the firstselect
above, rather than the second.