tags:

views:

166

answers:

3

Id like to perform an outer join with the second join statement in this query, I keep getting weird errors! (it must be the 3rd 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 neglected to say that I'm using SubSonic 3.0, the bug seems to be with SubSonic.....

A: 

See sample here

elder_george
+3  A: 

Performing an outer join requires two steps:

  1. Convert the join into a group join with into
  2. Use DefaultIfEmpty() on the group to generate the null value you expect if the joined result set is empty.

You will also need to add a null check to your 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)
              };
dahlbyk
When I do this I get: " The expression of type 'System.Linq.IQueryable`1[<>f__AnonymousType1`2[<>f__AnonymousType0`2[x,a],System.Collections.Generic.IEnumerable`1[w]]]' is not a sequence "
hminaya
What LINQ provider are you using? Are you sure it supports outer joins?
dahlbyk
I'm using LINQ 2 Objects
hminaya
What types are returned by Table1.All() and Table2.All()? Generally they should either both be IQueryable<_> or IEnumerable<_> - it looks like they're currently mixed, which might confuse the compiler.
dahlbyk
They both return the same type, it seems to be a SubSonic issue.
hminaya
This is the proper way to do an Outer Join in LINQ, my errors where caused by SubSonic
hminaya
A: 

Any progress on this problem? I'm experiencing it as well and have entered an issue into Subsonic's github project but I'm the only one who has commented on it. :)

bonder
I haven't had any follow up on this, I was hoping that rob connery would have seen this question already. Please provide the link to the issue you posted on github.
hminaya
It's here: http://github.com/subsonic/SubSonic-3.0/issues#issue/147I just saw Rob had done something in the repository about 4 hours ago so I need to check to see what is up.
bonder