question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Union not supported?

See original GitHub issue

SQL has a UNION operator. So I’m not sure I understand why Union() doesn’t appear to work.

var test = await DbContext.StorageAdjustments
    .Select(a => new StorageUpdateModel
    {
        TimeStamp = a.TimeStamp,
        Change = a.Volume,
        Volume = 0,
        Product = a.Product.Name,
        UpdateType = StorageUpdateType.Adjustment,
        Notes = $"{a.UserName}{(!string.IsNullOrWhiteSpace(a.Comments) ? $" : {a.Comments}" : null)}",
    })
    .Union(DbContext.StorageTrucks
        .Select(t => new StorageUpdateModel
        {
            TimeStamp = t.TimeStamp,
            Change = t.Volume,
            Volume = 0,
            Product = null,
            UpdateType = StorageUpdateType.Truck,
            Notes = $"BOL {t.Truck.FormattedBol}",
        }))
    .ToListAsync();
System.InvalidOperationException: 'Unable to translate set operation after client projection has been applied. Consider moving the set operation before the last 'Select' call.'
   at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ApplySetOperation(SetOperationType setOperationType, SelectExpression select2, Boolean distinct)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateUnion(ShapedQueryExpression source1, ShapedQueryExpression source2)
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_0`1.<ExecuteAsync>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetAsyncEnumerator(CancellationToken cancellationToken)
   at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable`1.GetAsyncEnumerator()
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.<ToListAsync>d__65`1.MoveNext()
   at Pegasus.Pages.Transloading.StorageLocations.DetailsModel.<OnGetAsync>d__15.MoveNext() in D:\Users\jwood\source\repos\Railtrax\Pegasus\Pages\Transloading\StorageLocations\Details.cshtml.cs:line 64

EF Core version: 6.06 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 6.0 Operating system: Windows 11 IDE: Visual Studio 2022 17.3.2)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
rojicommented, Sep 12, 2022

Thanks for the link. I wouldn’t assume that the plan/memory warning regarding CONVERT necessarily affects your case; at the very least I’d analyze the specific SQL here and see if the same warning is issued.

In any case, this is basically a dup of #19129 (and very specifically of https://github.com/dotnet/efcore/issues/19129#issuecomment-824148525).

1reaction
rojicommented, Sep 9, 2022

This isn’t related to the strings being different lengths is it?

No, definitely shouldn’t be. It should be the database type of one of the column there (Timestamp, Volume…). I’d put my money on a.Product.Name, which you’re trying to join with an empty string literal - try removing pairs until you isolate which one it is.

Read more comments on GitHub >

github_iconTop Results From Across the Web

UNION SQL statement not working
Hi, how do you add a 'where' clause to the overall query when you have a union. I have another query and the...
Read more >
I Can't Union “Part type not supported in CSG”?
Recently, I was trying to union some unions and parts but I surprised with a massaged pop up says: Here is my explorer:...
Read more >
sql server - CTE with UNION ALL not working as expected
The query below seems simple and straightforward, yet it produces unexpected results. CREATE TABLE #NUMBERS ( N BIGINT ); INSERT INTO #NUMBERS ...
Read more >
Union Your Data - Tableau
You can union your data to combine two or more tables by appending values (rows) from one table to another. To union your...
Read more >
SQL UNION overview, usage and examples
This article provides overview of the SQL UNION operator, ... The EXCEPT operator lists the rows in the first that are not in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found