如何正确地从IEnumerable <t>转换为List <t>?</t> </t>

时间:2012-04-30 09:32:35

标签: c# linq list ienumerable

我有这个LINQ

 var questions = _context.Questions
            .Where(q => q.Level.Level == level)
            .Select(q => new QuestionViewModel
            {
                Text = q.Text,
                Id = q.Id,
                IsMultiSelected = q.IsMultiSelected,
                AnswerViewModels = q.Answers
                                       .Select(
                                           a => new AnswerViewModel
                                                    {
                                                        Checked = false,
                                                        Text = a.Text,
                                                        Id = a.Id
                                                    }) as List<AnswerViewModel>
            });
        return questions.ToList();

我得到了

Exception Details: System.NotSupportedException: The 'TypeAs' expression with an input of type 'System.Collections.Generic.IEnumerable`1' and a check of type 'System.Collections.Generic.List`1' is not supported. Only entity types and complex types are supported in LINQ to Entities queries.

return questions.ToList();

我不在select中使用匿名类型。如何解决此错误?

更新

我编写了一些解决方案

    List<QuestionViewModel> result = new List<QuestionViewModel>();
    var questions = from q in _context.Questions
                    where q.Level.Level == level
                    select new QuestionViewModel()
                               {
                                   Text = q.Text,
                                   Id = q.Id,
                                   IsMultiSelected = q.IsMultiSelected,
                                   AnswerViewModels = from a in q.Answers
                                                      select new AnswerViewModel
                                                                 {
                                                                     Text = a.Text,
                                                                     Id = a.Id,
                                                                     Checked = false
                                                                 }
                               };
    var qList = questions.ToList();
    for(int i = 0; i < questions.Count(); i++)
    {
        var q = qList[i]; //question
        var a = q.AnswerViewModels.ToList(); //answers for question
        var answers = new List<AnswerViewModel>(); //List answers
        for(int j = 0; j < a.Count(); j++)
        {
            //add new Answer from IEnumerable<AnswerViewQuestion> to List<...>
            answers.Add(new AnswerViewModel
                            {
                                Checked = false,
                                Id = a[j].Id,
                                Text = a[j].Text
                            });
        }
        result.Add(q);
    }

如何优化?

2 个答案:

答案 0 :(得分:4)

问题在于

.Select(a => new AnswerViewModel { ... }) as List<AnswerViewModel>

应该是

.Select(a => new AnswerViewModel { ... }).ToList()

原因是将LINQ生成的IEnumerable转换为List的正确方法当然是调用ToList扩展方法,这个方法已经在最后给出了一条线。

答案 1 :(得分:1)

尝试这样的事情:

var questions = _context.Questions
            .Where(q => q.Level.Level == level)
            .Select(q => new QuestionViewModel
            {
                Text = q.Text,
                Id = q.Id,
                IsMultiSelected = q.IsMultiSelected,
                AnswerViewModels = q.Answers
                                       .Select(
                                           a => new AnswerViewModel
                                                    {
                                                        Checked = false,
                                                        Text = a.Text,
                                                        Id = a.Id
                                                    })
            }).AsEnumerable().Select(x => new QuestionViewModel
              {
                   Text = x.Text,
                   Id = x.Id,
                   IsMultiSelected = x.IsMultiSelected,
                   AnswerViewModels = x.Answers.ToList()
               });
        return questions.ToList();