如果我有问题编号列表
var questions = new List<int> { 2, 4, 8 };
是否有可能在 linq 中进行类似操作的查询?
var results = new List<Answer>();
foreach (var qNum in questions)
{
results.AddRange(Answers.Where(x => eval(x.Question+qNum) == "Yes"));
}
所以这将查找 x.Question2、x.Question4 和 x.Question8 的答案。
附录
目前我正在这样做:
var questionAnswers = new List<Answer>();
var Q2 = Questions.Where (x => x.MainQ2.ToUpper() == "NO")
.Select(x => new Answer { QuestionNumber = 2, AnswerText = x.MainTextQ2, User_Id = x.User_Id });
var Q4 = Questions.Where (x => x.MainQ4.ToUpper() == "NO")
.Select(x => new Answer { QuestionNumber = 4, AnswerText = x.MainTextQ4, User_Id = x.User_Id });
var Q8 = Questions.Where (x => x.MainQ8.ToUpper() == "NO")
.Select(x => new Answer { QuestionNumber = 8, AnswerText = x.MainTextQ8, User_Id = x.User_Id });
questionAnswers.AddRange(Q2);
questionAnswers.AddRange(Q4);
questionAnswers.AddRange(Q8);
questionAnswers.GroupBy (a => a.QuestionNumber).Dump();
public class Answer
{
public int QuestionNumber {get;set;}
public string AnswerText {get;set;}
public string User_Id {get;set;}
}
非常感谢 :)