私は私が選択して匿名型を使用していない正しくIEnumerableから<T>をリスト<T>に変換するには?
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.
を取得し、この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();
を持っています。このエラーを解決するには?私はいくつかの解決策
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