BILL
BILL

Reputation: 4869

How to correctly convert from IEnumerable<T> to List<T>?

I have this 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();

I get

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.

in

return questions.ToList();

I don't use anonymous types in select. How to resolve this error ?

UPDATE

I coded some solution

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

How to optimize ?

Upvotes: 1

Views: 1446

Answers (2)

Vajda
Vajda

Reputation: 1794

Try something like this:

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

Upvotes: 1

Jon
Jon

Reputation: 437376

The problem is with

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

It should be

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

The reason is that the correct way to convert a LINQ-generated IEnumerable to a List is of course by calling the ToList extension method, which you are already doing in the last line given.

Upvotes: 4

Related Questions