You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Using Func<> for where predicate does not work #12765
I'm trying to retrieve specific data from my table using a predicate. The researched sql query is as follow :
SELECT [plan].[Id] FROM [Plan] AS [plan] WHERE [plan].[IdCampagne] = 39
gives : SELECT [plan].[Id] FROM [Plan] AS [plan] WHERE [plan].[IdCampagne] = 39
NOT WORKING CODE
Func<Plan,bool>predicate= p =>p.IdCampagne==39;vara=_context.Plan.AsNoTracking().Where(predicate).Select(plan =>new{Id=plan.Id}).ToList();
gives : SELECT [p].[Id], [p].[IdCampagne], [p].[OtherStuff] FROM [Plan] AS [p]
Further technical details
EF Core version: 2.1.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows Server 2012 R2 Standard
IDE: Visual Studio 2017 15.7.5
You need to create lambda of Expression<Func<Plan, bool>>. Raw Func<> are opaque in expression tree and we cannot look what is inside. Using Expression<Func<>> integrate the lambda inside the expression with inner body.
I feel like the understanding of this behavior is far from evident, and as in one case the whole table is retrieved and in the other not, I guess this will cause problems in many architecture.
Isn't there maybe a way to promote a better understanding of this difference ?
The difference has nothing specific to EF Core as such. It is about difference in IQueryable vs IEnumerable. For any translation framework to work correctly, you would require IQueryable (Expression<Func<>>) type constructs. Above query would fail to work correctly even in other ORMs or linq to SQL translations.
If you have ideas about how to educate customers about this then please share.
Activity
smitpatel commentedon Jul 23, 2018
You need to create lambda of
Expression<Func<Plan, bool>>. RawFunc<>are opaque in expression tree and we cannot look what is inside. UsingExpression<Func<>>integrate the lambda inside the expression with inner body.[-]"Where" query does not work as expected in specific case[/-][+]Using Func<> for where predicate does not work[/+]bastien-besson commentedon Jul 24, 2018
Thanks @smitpatel for your fast answer.
I feel like the understanding of this behavior is far from evident, and as in one case the whole table is retrieved and in the other not, I guess this will cause problems in many architecture.
Isn't there maybe a way to promote a better understanding of this difference ?
smitpatel commentedon Jul 24, 2018
The difference has nothing specific to EF Core as such. It is about difference in IQueryable vs IEnumerable. For any translation framework to work correctly, you would require IQueryable (Expression<Func<>>) type constructs. Above query would fail to work correctly even in other ORMs or linq to SQL translations.
If you have ideas about how to educate customers about this then please share.