【问题标题】:Linq, join and null how to check a valueLinq,join和null如何检查值
【发布时间】:2015-06-03 11:07:34
【问题描述】:

我尝试将两个集合合二为一。如果我的第二个是空的,我只需要一个空值,这是我的代码(如果com 不为空,则正确)

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

我想得到AnswerSet(g, Content)AnswerSet(g, null) 的问题,我猜是g.Key equals c.UserID 当com 为空时

【问题讨论】:

标签: c# linq windows-store-apps


【解决方案1】:

基本上你想要的是left outer join。你可以通过使用join into 而不仅仅是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);

【讨论】:

  • 谢谢,这太完美了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-07
  • 1970-01-01
  • 2017-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多