Outer join in LINQ task

I like doing outer join with the second join operator in this query, I keep getting weird errors! (this should be the third RedBull)

var Objeto = from t in Table1.All()
             join su in table2.All() on t.Id equals su.Id
             join tab2 in Table1.All() on t.PId equals tab2.Id //<-I want it here
             select new
             {
                 t.Field1,
                 SN = su.Field123,
                 PTN = tab2.FieldABC
              };

      

Any help would be appreciated.

[Edit] - I forgot to say that I am using SubSonic 3.0, the bug seems to be related to SubSonic .....

+2


source to share


1 answer


Making an outer join requires two steps:

  • Convert join to group join with into

  • Use DefaultIfEmpty()

    in a group to generate the value null

    you expect if the merged result set is empty.


You also need to add a check null

for select

.

var Objeto = from t in Table1.All()
             join su in table2.All() on t.Id equals su.Id
             join tab2 in Table1.All() on t.PId equals tab2.Id into gj
             from j in gj.DefaultIfEmpty()
             select new
             {
                 t.Field1,
                 SN = su.Field123,
                 PTN = (j == null ? null : j.FieldABC)
              };

      

+4


source







All Articles