LINQ - Full Outer Join

后端 未结 16 1568
既然无缘
既然无缘 2020-11-21 22:45

I have a list of people\'s ID and their first name, and a list of people\'s ID and their surname. Some people don\'t have a first name and some don\'t have a surname; I\'d l

16条回答
  •  爱一瞬间的悲伤
    2020-11-21 22:57

    Full outer join for two or more tables: First extract the column that you want to join on.

    var DatesA = from A in db.T1 select A.Date; 
    var DatesB = from B in db.T2 select B.Date; 
    var DatesC = from C in db.T3 select C.Date;            
    
    var Dates = DatesA.Union(DatesB).Union(DatesC); 
    

    Then use left outer join between the extracted column and main tables.

    var Full_Outer_Join =
    
    (from A in Dates
    join B in db.T1
    on A equals B.Date into AB 
    
    from ab in AB.DefaultIfEmpty()
    join C in db.T2
    on A equals C.Date into ABC 
    
    from abc in ABC.DefaultIfEmpty()
    join D in db.T3
    on A equals D.Date into ABCD
    
    from abcd in ABCD.DefaultIfEmpty() 
    select new { A, ab, abc, abcd })
    .AsEnumerable();
    

提交回复
热议问题