Linq方法检查一组属性对另一组属性

时间:2015-12-07 16:35:12

标签: c# linq

所以我需要根据数据库中的正确答案检查问题的答案。

import Foo._

我知道我可以这样做:

Obj1.Answer == Obj2.CorrectAnswer

但有没有办法在一个linq语句中执行此操作,其中Obj1.Answer == Obj2.CorrectAnswer返回匹配的对象列表?这段代码对我来说很好,我只是好奇,如果有一个linq方式去做 - 如果有,我似乎无法弄明白。

3 个答案:

答案 0 :(得分:3)

我认为你的答案就在这里:

currectAnswers.AddRange(testQuestions.Where(
t=>listOfQ.Any(l=>l.QuestionId==t.QuestionId && 
l.Answer==t.CorrectAnswer))

答案 1 :(得分:2)

correctAnswers=listOfQ.Join(testQuestions,
  a=>new {a.QuestionId,a.Answer},
  b=>new {b.QuestionId.ToString(),b.CorrectAnswer.ToString()},
  (a,b)=>b);

答案 2 :(得分:0)

我们的模型类:

| ProductId | sum |
|-----------|-----|
|         A |  46 |
|         B |  18 |
|         C |  34 |

主:

public class Question
    {
        public int Iduser;
        public int IdQuestion;
        public string Answer;
        public Question(int iduser,int idquestion,string answer)
        {
            this.Iduser = iduser;
            this.IdQuestion = idquestion;
            this.Answer = answer;
        }
    }

public class SimpleQuestion
{
    public int IdQuestion;
    public string Answer;
    public SimpleQuestion(int idquestion, string answer)
    {
        this.IdQuestion = idquestion;
        this.Answer = answer;
    }
}