Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
916 views
in Technique[技术] by (71.8m points)

ef core 3.1 - Helper methods like Any() , Contains() when used inside FUNC predicate in where clause are not getting translated to Sql Server and hence erred out

I am using .net core 3 with ef. When I use the below code, result is loaded

DbSet<User>().Where(User =>User.UserRoleMapping.Any(UserRoleMapping => UserRoleMapping.RoleID == (int)1))

? But when I use predicate inside the where clause of dbset rather than an expression, here’s the error encountered


The LINQ expression 'DbSet().Where(u => User.UserRoleMapping.Any(UserRoleMapping => UserRoleMapping.RoleID == 1))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038?for more information.


Looks like the Any(), Contains() and all the helper methods are not getting translated to server execution due to limitation in Core 3.0 onwards, client side translation is restricted to top level projection only.

Is there any way I can progress with predicate without running into any problem.

Any insights?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...