LINQKit
LINQKit copied to clipboard
Nested select expression give error Expression of type 'System.Func`2[X,Y]' cannot be used for parameter of type 'System.Linq.Expressions.Expression`1[System.Func`2[X,Y]]'
When I tried the following code I get an runtime error. I looks that the asExpandable is not working for select
private async Task<List<SurveyEditChapter>> GetSurveyChapters(int surveyId)
{
return await _ctx.SurveyChapter.AsNoTracking().AsExpandable().Where(x => x.SurveyId == surveyId)
.Select(x => new SurveyEditChapter
{
SurveyChapterId = x.Id,
SequenceNumber = x.SequenceNumber,
Name = x.Name,
Description = x.Description,
Questions = x.ChapterQuestions.Select(Projector().Compile()).OrderBy(z => z.SequenceNumber).ToList()
})
.OrderBy(x => x.SequenceNumber).ToListAsync();
}
private static Expression<Func<ChapterQuestion, SurveyEditQuestion>> Projector()
{
return y => new SurveyEditQuestion
{
ChapterQuestionId = y.Id,
QuestionId = y.Question.Id,
Code = y.Code,
Mandatory = y.Mandatory,
Visible = y.Visible,
ChartTypeFlag = y.ChartTypeFlags,
QuestionTypeFlag = y.QuestionTypeFlags,
SequenceNumber = y.SequenceNumber,
Name = y.Question.Name,
Description = y.Question.Description,
QuestionTypeFlags = y.Question.QuestionTypeFlags,
ChartTypeFlags = y.Question.ChartTypeFlags,
Answers = y.Question.QuestionAnswers.Select(z => new QuestionEditAnswer
{
QuestionAnswerId = z.Id,
Value = z.Value,
Description = z.AnswerText.Description,
ImageId = z.AnswerImage.Id
}).ToList()
};
}
This has just caught me out. Did you manage to get around this issue?
I didn't not use it anymore. Probably I rewrite some linq queries
In case another newbie like me ends up here with a similar issue: for me, the root cause was that I was trying to generate an expression inline, like this:
query = query
.Where(o => o.OrderPositions.Any(HasBuildingIsAssignedToUserDirectly(userId).Compile()));
What actually worked for me instead was to split up the procedure:
var filterExpression = HasBuildingIsAssignedToUserDirectly(userId);
query = query
.Where(o => o.OrderPositions.Any(filterExpression.Compile()));
I'm seeing something similar in the above example as well.