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
Set operations: support when placed after client evaluation in projection #16243
the following query is working as expected but fails in combination with union with the exception System.InvalidOperationException: 'Can't process set operations after client evaluation, consider moving the operation before the last Select() call (see issue #16243)':
when removing Comments selection the query works fine within union.
Is this the same issue like from @roji 's example as the comments selection itself is not a client evaulation?
When using InMemoryDatabase instead of SQL this works fine.
Yes, your query is the same as what I posted originally. You should be able to rewrite this query to work by moving the Union earlier in your query, which also makes for less duplication:
(of course, in your particular query you don't need a Union at all since you're just doing it over IsArchived true/false, but I'm assuming this is only a minimal example of some sort).
Your example is not complete because you request on same table
I have the use case with two tables with one different column but i want to make a union in a common object. I Can't I have the same error
varqueryableA=Context.A.select( x =>newUnionClass{Name=x.NameA,Description=x.DescriptionA);varqueryableB=Context.Bselect( x =>newUnionClass{Name=x.NameB,Description=x.DescriptionB);varqueryUnion=queryableA.Union(queryableB)varresult=queryUnion.Select( x=>x.Name.contains("RE));
Reacted by John Vert, JimFarr, Bert-Jan De Spiegeleer, Shelly Chen, Temoxa, Mark Bentley @ VizworX, DK, Mahdi Mahzouni, Adrian Schiller, Leonid Kurepin and 25 more
@Maleaume@brandonsmith86@BeejeeDS the only issue here is with set operations after a client projection - as long as you write your query to place the union before that, there's no issue. You can typically do this by projecting to an anonymous type (which works fine and doesn't require client translation) and doing everything you need on that. If you need to project out to your own type, do that at the end.
For example:
varresult=ctx.A.Select(a =>new{Name=a.NameA,Description=a.DescriptionA}).Union(ctx.B.Select(b =>new{Name=b.NameB,Description=b.DescriptionB})).Where(x =>x.Name.StartsWith("a"))// Add any further operations to be executed in the database here.Select(x =>newUnionClass{Name=x.Name,Description=x.Description}).ToList();
Reacted by Joshua Grant, Mahdi Mahzouni, Justin Maier, Alexander Leontiev, Stuart Dobson, Yan Pitangui, Nadeera Kuruppu, Andrei Tudose, Valentin Dimitroff, Alexandre Junges and 2 more
I have the same issue but I'm trying Union for two Join queries.
varresult=await_context.Stage.Join(_context.User, s =>s.UserNo, u =>u.UserNo,(s,u)=>new{s,u}).Select(c =>new{Detail=$"{c.s.Description.Trim()} - {c.u.UserDescription.Trim()}",Id=c.s.Id}).Union(_context.Stage.Join(_context.UserGroup, s =>s.UserGroupNo, ug =>ug.UserGroupNo,(s,ug)=>new{s,ug}).Select(c =>new{Detail=$"{c.s.Description.Trim()} - {c.ug.GroupDesription.Trim()}",Id=c.s.Id})).Where(c =>c.Id==Id).Select(x =>x.Detail).ToListAsync();
and I get the same error. Am I doing anything wrong here? Please assist
@fingers10's error repros for me, although removing the Union shows the query working without any sort of client evaluation. @smitpatel any ideas?
Repro code
classProgram{staticasyncTaskMain(string[]args){usingvarctx=newBlogContext();ctx.Database.EnsureDeleted();ctx.Database.EnsureCreated();varresult=awaitctx.Stage.Join(ctx.User, s =>s.UserNo, u =>u.UserNo,(s,u)=>new{s,u}).Select(c =>new{Detail=$"{c.s.Description.Trim()} - {c.u.UserDescription.Trim()}",Id=c.s.Id}).Union(ctx.Stage.Join(ctx.UserGroup, s =>s.UserGroupNo, ug =>ug.UserGroupNo,(s,ug)=>new{s,ug}).Select(c =>new{Detail=$"{c.s.Description.Trim()} - {c.ug.GroupDesription.Trim()}",Id=c.s.Id})).Where(c =>c.Id==8).ToListAsync();}}publicclassBlogContext:DbContext{publicDbSet<Stage>Stage{get;set;}publicDbSet<User>User{get;set;}publicDbSet<UserGroup>UserGroup{get;set;}staticILoggerFactoryContextLoggerFactory=>LoggerFactory.Create(b =>b.AddConsole().AddFilter("",LogLevel.Information));protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder)=>optionsBuilder.UseSqlServer(@"Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0").EnableSensitiveDataLogging().UseLoggerFactory(ContextLoggerFactory);}publicclassStage{publicintId{get;set;}publicintUserNo{get;set;}publicintUserGroupNo{get;set;}publicstringDescription{get;set;}}publicclassUser{publicintId{get;set;}publicintUserNo{get;set;}publicstringUserDescription{get;set;}}publicclassUserGroup{publicintId{get;set;}publicintUserNo{get;set;}publicstringGroupDesription{get;set;}publicintUserGroupNo{get;set;}}
@fingers10 you'll need to rewrite your query to move the trimming out to after the Select:
varresult=awaitctx.Stage.Join(ctx.User, s =>s.UserNo, u =>u.UserNo,(s,u)=>new{s,u}).Select(c =>new{Detail=c.s.Description,Description=c.u.UserDescription,Id=c.s.Id}).Union(ctx.Stage.Join(ctx.UserGroup, s =>s.UserGroupNo, ug =>ug.UserGroupNo,(s,ug)=>new{s,ug}).Select(c =>new{Detail=c.s.Description,Description=c.ug.GroupDesription,Id=c.s.Id})).Where(c =>c.Id==8).Select(c =>new{Detail=$"{c.Detail.Trim()} - {c.Description.Trim()}"}).ToListAsync();
varqueryableA=Context.A.select( x =>newUnionClass{Name=x.NameA,Description=x.DescriptionA);varqueryableB=Context.Bselect( x =>newUnionClass{Name=x.NameB,Description=x.DescriptionB);varqueryUnion=queryableA.Union(queryableB)varresult=queryUnion.Select( x=>x.Name.contains("RE));
I have the same issue with inheritance. I guess it would be a use case for TPC but it is not supported yet and I use efcore 3 (next version doesn't have Oracle driver yet). I have an existing db with multiple tables that share 90% of their fields.
I have something like this:
classBaseClass{publicstringName;publicstringDescription;}[TableA]classA:BaseClass{publicstringFieldA;}[TableB]classB:BaseClass{publicstringFieldB;}varqueryableA=Context.A.select( x =>(BaseClass)x);varqueryableB=Context.B.select( x =>(BaseClass)x);varqueryUnion=queryableA.Union(queryableB)varresult=Context.C.Where(c =>queryUnion.Select(u =>u.Name).Contains(c.Name));
The same request works fine if I retrieve the Name field instead of BaseClass. However, if I need 2 fields I am doomed!
I spend lot of time to make complex SQL queries compatible with efcore where I would have written by hand the same SQL request 10x faster ; this is the downside of ORM and multi-database compatibility.
Reacted by Chrysostomos Kolovos, grishinalexey and YasserKharab
@xrkolovos This issue is in the Backlog milestone. This means that it is not planned for the next release (EF Core 6.0). We will re-assess the backlog following the this release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources.
For users arriving here, most of problematic user queries we've seen involve client evaluation (i.e. constructs which can't be translated to SQL) which can simply be moved after the set operation; that is the optimal solution (see above for examples).
The remaining cases where this cannot be done involve e.g. client evaluation only on one side of the set operation (e.g. the OP). Translating this would be possible only by synthesizing a discriminator allowing us to know which row comes from which side, and applying the client evaluation based on that. While possible, this is quite complex and we haven't seen any actual request for it.
For the record, for Concat (UNION ALL) it's also possible to evaluate the operator on the client-side, since it doesn't involve bring any excess data to the client. This isn't true for the other set operations.
Activity
cyptus commentedon Mar 12, 2020
the following query is working as expected but fails in combination with union with the exception
System.InvalidOperationException: 'Can't process set operations after client evaluation, consider moving the operation before the last Select() call (see issue #16243)':is working, but failing with union:
when removing
Commentsselection the query works fine within union.Is this the same issue like from @roji 's example as the comments selection itself is not a client evaulation?
When using InMemoryDatabase instead of SQL this works fine.
Full example code: https://gist.github.com/cyptus/ef80cb7a58aa08ab06da4cd42da1ec06
roji commentedon Mar 15, 2020
Yes, your query is the same as what I posted originally. You should be able to rewrite this query to work by moving the Union earlier in your query, which also makes for less duplication:
(of course, in your particular query you don't need a Union at all since you're just doing it over IsArchived true/false, but I'm assuming this is only a minimal example of some sort).
Maleaume commentedon Mar 31, 2020
Your example is not complete because you request on same table
I have the use case with two tables with one different column but i want to make a union in a common object. I Can't I have the same error
I would like to
brandonsmith86 commentedon Mar 31, 2020
I have the same use-case as @Maleaume. How do you suggest we workaround the issue in this case?
BeejeeDS commentedon May 1, 2020
I have the same scenario as @Maleaume.
I even want to add paging to it, so i'll be needing a Skip and Take after the Union.
roji commentedon May 1, 2020
@Maleaume @brandonsmith86 @BeejeeDS the only issue here is with set operations after a client projection - as long as you write your query to place the union before that, there's no issue. You can typically do this by projecting to an anonymous type (which works fine and doesn't require client translation) and doing everything you need on that. If you need to project out to your own type, do that at the end.
For example:
fingers10 commentedon May 11, 2020
I have the same issue but I'm trying
Unionfor twoJoinqueries.and I get the same error. Am I doing anything wrong here? Please assist
roji commentedon May 11, 2020
@fingers10's error repros for me, although removing the Union shows the query working without any sort of client evaluation. @smitpatel any ideas?
Repro code
smitpatel commentedon May 11, 2020
Interpolated string is client evaluation.
roji commentedon May 11, 2020
Of course, I somehow missed that, thanks...
@fingers10 you'll need to rewrite your query to move the trimming out to after the Select:
FWIW that also removes the needless duplication.
fingers10 commentedon May 12, 2020
@roji I'm getting this error - Set operations over different store types are currently unsupported, after rewriting the query as you mentioned.
roji commentedon May 12, 2020
@fingers10 you need to post your full query (and preferably model as well) for me to be able to help. The query I posted above worked for me.
13 remaining items
nmoreaud commentedon Feb 24, 2021
I have the same issue with inheritance. I guess it would be a use case for TPC but it is not supported yet and I use efcore 3 (next version doesn't have Oracle driver yet). I have an existing db with multiple tables that share 90% of their fields.
I have something like this:
The same request works fine if I retrieve the Name field instead of BaseClass. However, if I need 2 fields I am doomed!
I spend lot of time to make complex SQL queries compatible with efcore where I would have written by hand the same SQL request 10x faster ; this is the downside of ORM and multi-database compatibility.
xrkolovos commentedon Aug 3, 2021
Hello.
Any thoughs on this issue? Is by any chance going to be in .net 6 wave?
ajcvickers commentedon Aug 3, 2021
@xrkolovos This issue is in the Backlog milestone. This means that it is not planned for the next release (EF Core 6.0). We will re-assess the backlog following the this release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources.
Pentadome commentedon May 20, 2022
For me,
DateTimeOffset.ToString()caused.Concats to fail. HoweverDateTimeOffset.Day.Tostring() + "-" + DateTimeOffset.Month.Tostring() + "-" + DateTimeOffset.Year.Tostring()did work.Kinda silly, but I hope it helped someone
roji commentedon May 24, 2022
For users arriving here, most of problematic user queries we've seen involve client evaluation (i.e. constructs which can't be translated to SQL) which can simply be moved after the set operation; that is the optimal solution (see above for examples).
The remaining cases where this cannot be done involve e.g. client evaluation only on one side of the set operation (e.g. the OP). Translating this would be possible only by synthesizing a discriminator allowing us to know which row comes from which side, and applying the client evaluation based on that. While possible, this is quite complex and we haven't seen any actual request for it.
For the record, for Concat (UNION ALL) it's also possible to evaluate the operator on the client-side, since it doesn't involve bring any excess data to the client. This isn't true for the other set operations.