Linq基于有孩子(parentId)将集合拆分为两个

时间:2016-10-19 21:01:08

标签: c# linq

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

boolean archerPlacementMode = false;
....
myButton.setOnMouseClicked(new EventHandler<MouseEvent>(){
    public void handle(MouseEvent e)
            {
                if(!archerPlacementMode) {
                     archerPlacementMode = true;
                     gc.setFill(color);
                     gc.fillRect(0,0,width,height);
                     archerPlacementMode = true;
                      return;
                }
            }
        });

scene.setOnMouseClicked(new EventHandler<MouseEvent>() {
    public void handle(MouseEvent e)
    {
        if(archerPlacementMode) {
            archer.setX((int)e.getSceneX());
            archer.setY((int)e.getSceneY());
            archer.drawCharStand(gc);
            archerPlacementMode = false;
        }
    }
});

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

列出Question int QuestionId string Text int? ParentId int? UserId List<Question> allQuestions = db.Fetch<Question>(@"Select * FROM Question"); questionsAndAnswers_NotAnsweredByUserId5 = allQuestions。(Linq?)

列出<Question> questionsAndAnswers_AnsweredByUserId5 = allQuestions。(Linq?)

1 个答案:

答案 0 :(得分:0)

var byThatUser = allQuestions.Where(q => q.ParentId == certainUserId).ToList();

var notByThatUser = allQuestions.Where(q => q.ParentId != certainUserId).ToList();