는 내가 선택에 익명 형식을 사용하지 않는이 LINQ는IEnumerable <T>을 정확히 <T> 목록으로 변환 하시겠습니까?
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();
내가
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.
를 얻을 수 있습니다. 이 오류를 해결하는 방법? 나는 몇 가지 솔루션
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);
}
방법을 최적화 할 수
코딩
당신이 존의 제안을 사용하는 경우, 당신은 단지 질문에 반환해야 명확히하기 : 반환 질문을; –
작동하지 않습니다. 'LINQ to Entities가'System.Collections.Generic.List'1 메서드를 인식하지 못합니다. [ExpertApplication.ViewModels.AnswerViewModel] ToList [AnswerViewModel] (System.Collections.Generic.IEnumerable'1 [ExpertApplication.ViewModels.AnswerViewModel]) '메서드 이 메서드는 저장소 식으로 변환 할 수 없습니다. ' – BILL
나는'http://stackoverflow.com/questions/3800834/nested-linq-returning-a-this-method-cannot-be-translated-into-a-store-expression '이라는 문구를 검색하지만 그 대답은 효과가 없다. 나를 위해 – BILL