c# - 如何正确地将 IEnumerable<T> 转换为 List<T>?

标签 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();

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

更新

我编写了一些解决方案

    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);
    }

如何优化?

最佳答案

问题在于

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

应该是

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

原因是将 LINQ 生成的 IEnumerable 转换为 List 的正确方法当然是调用 ToList 扩展方法,你已经在最后一行中做了。

关于c# - 如何正确地将 IEnumerable<T> 转换为 List<T>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10381382/

相关文章:

c# - LINQ 查询在请求字节时返回 null

c# - 使用 dapper c# 填充列表对象

java - 将两个列表结构链接在一起

c# - 确定 (x,y,z) 点是否在由点数组定义的形状内

c# - 每次构建时,其中一个程序集都会从我的解决方案中删除

c# - Kendo 小部件和 Bootstrap 输入组插件

c# - Dictionary<Foo, List<Bar>> 如何使用 LINQ 将列表分配给键的属性?

c# - 帮助将 LINQ 表达式从 C# 转换为 VB

C# 列表,从对象列表创建列表对象

c# - 使用 C# 的 Metro 风格应用教程