Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested select expression give error Expression of type 'System.Func2[X,Y]' cannot be used for parameter of type 'System.Linq.Expressions.Expression1[System.Func`2[X,Y]]' #108

Open
Anderman opened this issue Nov 11, 2019 · 3 comments

Comments

@Anderman
Copy link

Anderman commented Nov 11, 2019

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()
            };
        }
@kyletindle
Copy link

This has just caught me out. Did you manage to get around this issue?

@Anderman
Copy link
Author

Anderman commented Jan 7, 2022

I didn't not use it anymore. Probably I rewrite some linq queries

@joshuajung
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants