Answer the question
In order to leave comments, you need to log in
LINQ in C#: what happens to a Func that is nested in an Expression?
The user has an organization, the organization has users. We use this code:
_dbContext.Organizations.Where(o => o.Name.Contains("Roga") && o.Users.Any(u => u.FirstName == "Petr")).Select(...)
FROM [dbo].[Organizations] AS [Extent1]
WHERE ([Extent1].[Name] LIKE N'%Roga%') AND ( EXISTS (SELECT
1 AS [C1]
FROM [dbo].[AspNetUsers] AS [Extent3]
WHERE ([Extent1].[Id] = [Extent3].[OrganizationId]) AND (N'Petr' = [Extent3].[FirstName])
))
Func<User, bool> func = u => u.FirstName == "Petr";
return _dbContext.Organizations.Where(o => o.Name.Contains("Roga") && o.Users.Any(func)).Select(...)
Internal .NET Framework Data Provider error 1025.
Answer the question
In order to leave comments, you need to log in
As I understand it, this problem of the impossibility of taking out a nested lambda into a separate variable is caused by the stupidity of the sisharp compiler? Because could he recognize at compile time that we are passing a Func variable to Any, and instead of an expression with a reference to the variable, generate an expression with a nested lambla that he would read from the variable at compile time? But instead it creates an expression with a reference to the variable, causing the parser to crash?
I'm not sure, but my version is the following. In the first case, there is no lambda, there is a continuous expression of the following form:
o => o.Name.Contains("Roga") && o.Users.Any(u => u.FirstName == "Petr")
those. (u => u.FirstName == "Petr") is an expreshin nested within an expreshin.
In the second case, there is a real lyabda that is transmitted from outside, so it swears.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question