如何使用LINQ更新我的模型代码

时间:2013-09-22 10:37:05

标签: c# linq

这是我的模型代码

public class Question
    {
        public int ID { set; get; }
        public string QuestionText { set; get; }

        public List<Answer> Answers { set; get; }
        [Required]
        public string SelectedAnswer { set; get; }

        public Question()
        {
            Answers = new List<Answer>();
        }
    }

    public class Answer
    {
        public int ID { set; get; }
        public string AnswerText { set; get; }
        public string IsSelected { set; get; }
    }

    public class Evaluation
    {
        public List<Question> Questions { set; get; }
        public Evaluation()
        {
            Questions = new List<Question>();
        }
    }

这样我正在更新我的模型

public void Edit(Evaluation model)
        {
            var evalVM = new Evaluation();
            var q1 = new Question { ID = 1, QuestionText = "What is your favourite language" };
            q1.Answers.Add(new Answer { ID = 12, AnswerText = "PHP", IsSelected = "" });
            q1.Answers.Add(new Answer { ID = 13, AnswerText = "ASP.NET", IsSelected = "" });
            q1.Answers.Add(new Answer { ID = 14, AnswerText = "Java", IsSelected = "" });
            evalVM.Questions.Add(q1);

            var q2 = new Question { ID = 2, QuestionText = "What is your favourite DB" };
            q2.Answers.Add(new Answer { ID = 16, AnswerText = "SQL Server", IsSelected = "" });
            q2.Answers.Add(new Answer { ID = 17, AnswerText = "MySQL", IsSelected = "" });
            q2.Answers.Add(new Answer { ID = 18, AnswerText = "Oracle", IsSelected = "" });
            evalVM.Questions.Add(q2);

            foreach (var a in model.Questions)
            {
                var selectedVal = a.SelectedAnswer;
                foreach (var x in evalVM.Questions)
                {
                    var Answer = x.Answers;
                    foreach (var y in Answer)
                    {
                        if (y.ID.ToString() == selectedVal)
                        {
                            y.IsSelected = "checked";
                        }
                    }
                }
            }
}

这里只看到这一行我在foreach循环中迭代并在循环中更新我的模型

foreach (var a in model.Questions)
            {
                var selectedVal = a.SelectedAnswer;
                foreach (var x in evalVM.Questions)
                {
                    var Answer = x.Answers;
                    foreach (var y in Answer)
                    {
                        if (y.ID.ToString() == selectedVal)
                        {
                            y.IsSelected = "checked";
                        }
                    }
                }
            }

我不想迭代循环来更新我的模型,而是想通过LINQ来实现。请指导我需要写的内容。

1 个答案:

答案 0 :(得分:2)

foreach (var selectedVal in model
                            .Questions
                            .Select(q => q.SelectedAnswer))

  foreach (var x in evalVM
                      .Questions
                      .SelectMany(q => q.Answers)
                      .Where(answer => answer.ID.ToString() == selectedVal))

    x.IsSelected = "checked";

var selectedValues = model
                     .Questions
                     .Select(q => q.SelectedAnswer);

foreach (var x in evalVM
                  .Questions
                  .SelectMany(q => q.Answers)
                  .Where(answer => selectedValues.Contains(answer.ID.ToString())))                    
  x.IsSelected = "checked";