开发者

Outer join in LINQ Problem

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
           开发者_JAVA百科   };

Any help would be appreciated.

[Edit] - I neglected to say that I'm using SubSonic 3.0, the bug seems to be with SubSonic.....


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)
              };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜