【问题标题】:Linq split collection into two based on having children (parentId)Linq 根据有孩子(parentId)将集合分成两部分
【发布时间】:2016-10-20 05:03:23
【问题描述】:

假设模型(用于问题和问题的答案)

Question
int QuestionId
string Text
int? ParentId
int? UserId

List<Question> allQuestions = db.Fetch<Question>(@"Select * FROM Question");

如何根据 ParentId 和 UserId 将上面的问题集合拆分为两个集合。第一个集合包含由某个 UserId 回答的问题,而第二个集合包含没有该 UserId 回答的问题?

List&lt;Question&gt; questionsAndAnswers_NotAnsweredByUserId5 = allQuestions.(Linq?)

List&lt;Question&gt; questionsAndAnswers_AnsweredByUserId5 = allQuestions.(Linq?)

【问题讨论】:

    标签: c# linq


    【解决方案1】:
    var byThatUser = allQuestions.Where(q => q.ParentId == certainUserId).ToList();
    
    var notByThatUser = allQuestions.Where(q => q.ParentId != certainUserId).ToList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-29
      • 2013-02-19
      • 1970-01-01
      • 2015-02-20
      • 2021-07-17
      • 1970-01-01
      • 2017-07-17
      • 1970-01-01
      相关资源
      最近更新 更多