0

I've created this with LINQ

var main = (from p in participants
            from cc in participants
            where cc.Id != p.Id
            select new { Id1 = p.Id, Id2 = cc.Id });

I was able to recreate it to..

var main = participants.SelectMany(n => participants, (x, y) => new { Id1 = x.Id, Id2 = y.Id })
                       .Where(x => x.Id1 != x.Id2).ToList();

Same think I want to do with...

var test = (from m in main
            join cr in db.ContactRelations on new { Id1 = m.Id1, Id2 = m.Id2 } equals new { Id1 = cr.ContactId, Id2 = cr.RelationContactId } into tt
            from cr in tt.DefaultIfEmpty()
            select new { m.Id1, m.Id2, found = cr == null ? false : true }).ToList();

So far, It's not left join but only join.. and that null values is what I need..

    var test = main.Join(db.ContactRelations, 
                c => new { Id1 = c.Id1, Id2 = c.Id2 }, 
                x => new { Id1 = x.ContactId, Id2 = x.RelationContactId }, 
                (c, x) => new { c.Id1, c.Id2, found = x == null ? false : true }).ToList();

Is possible to implement DeafultIfEmpty() somehow ?

Thanks..

Tomas P.
  • 161
  • 2
  • 10
  • 2
    this is still linq... Think you mean replacing the query syntax by the method syntax ? By the way, I think the query syntax is much more readable for join than method syntax (but that's just a point of view, which has probably nothing to do here). – Raphaël Althaus May 12 '14 at 14:28
  • I think this answer your question. http://stackoverflow.com/questions/9171063/convert-sql-to-linq-left-join-with-null – Barry West May 12 '14 at 14:40
  • Thank you guys, I admit now that query syntax is looking better, but I found solution var ttw = main.GroupJoin(db.ContactRelations, c => new { Id1 = c.Id1, Id2 = c.Id2 }, x => new { Id1 = x.ContactId, Id2 = x.RelationContactId }, (f, b) => new { f, b }).ToList(); .SelectMany(z => z.b.DefaultIfEmpty(), (z, g) => new { z, g }).ToList(); – Tomas P. May 14 '14 at 07:34

0 Answers0