0

I try to join two collection into one. If my second is empty I just need a null value, here is my code (it's correct if com is not empty)

    var tmp = List{ Elem {long UserID; string tmpContent} };
    var com = List{ Comment{long UserID; string Content} } ;

    var res = from t in tmp
              group t by t.UserID into g
              join c in com on g.Key equals c.UserID
              select new AnswerSet(new List<Answer>(g), c.Content);

I would like to get AnswerSet(g, Content) ou AnswerSet(g, null) the problem, I guess, is with g.Key equals c.UserID when com is empty

Adrien Blaizot
  • 47
  • 1
  • 2
  • 11

1 Answers1

6

Basically what you want is a left outer join. you can do that by using join into instead of just join.

var res = from t in tmp
          group t by t.UserID into g
          join c in com on g.Key equals c.UserID into j
          from subc in j.DefaultIfEmpty()
          select new AnswerSet(new List<Answer>(g), subc != null ?subc.Content : null);
Kryptos
  • 875
  • 8
  • 19