How to do a full outer join in Linq?

后端 未结 5 1948
花落未央
花落未央 2020-12-02 23:01

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:

5条回答
  •  甜味超标
    2020-12-02 23:37

    I think I have the answer here, which is not as elegant as I'd hoped, but it should do the trick:

    var studentIDs = StudentClasses.Select(sc => sc.StudentID)
      .Union(StudentTeachers.Select(st => st.StudentID);
      //.Distinct(); -- Distinct not necessary after Union
    var q =
      from id in studentIDs
      join sc in StudentClasses on id equals sc.StudentID into jsc
      from sc in jsc.DefaultIfEmpty()
      join st in StudentTeachers on id equals st.StudentID into jst
      from st in jst.DefaultIfEmpty()
      where st == null ^ sc == null
      select new { sc, st };
    

    You could probably squeeze these two statements into one, but I think you'd sacrifice code clarity.

提交回复
热议问题