var test = await Db.Tests.Include(test => test.Questions)
.ThenInclude(ques => ques.Choices.Select(
ch => new {
ch.Id, ch.OptionName,
ch.OptionText, ch.OptionDetails,
ch.IsAnswer, ch.QuestionId
})).AsNoTracking()
.FirstOrDefaultAsync(z => z.TestId == testId);
Without Select clause it works fine. But I don't need all the Choices properties, so I try to select some of the properties by using Select clause.
It throughs this error:
System.InvalidOperationException: Lambda expression used inside Include is not valid.
Can anybody tell me what's wrong here?
I think you want something like:
var test = await Db.Tests
.Where(test => test.TestId == testId)
.Include(test => test.Questions) // Shouldn't need this
.ThenInclude(ques => ques.Choices) // Shouldn't need this
.SelectMany(test => test.Questions.SelectMany(ques => ques.Choices.Select(ch => new {
ch.Id,
ch.OptionName,
ch.OptionText,
ch.OptionDetails,
ch.IsAnswer,
ch.QuestionId
}))))
.AsNoTracking();
This will give you all the choices for a particular test.
But... since you're materializing the results as a lambda, you actually shouldn't need either Include
. (EF will give warnings saying that it ignored your Include
in the console anyway).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.