-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Union not supported? #29027
Comments
@SoftCircuits EF Core does translate Union(). However, in the example above you're using string interpolation (`$"..."), which is something that isn't translatable to SQL; since that occurs before the Union() operator, EF isn't able to translate that to SQL (as the exception says). You should be able to get this to work by avoiding string interpolation above, e.g. replace Another option is to project out to a temporary anonymous type, and project again after the Union() operator to perform the string interpolation. |
@roji Thanks, I guess I knew that could be a potential issue, but I was expecting a different error there. However, when I change it, I continue to have issues. var test = await DbContext.StorageAdjustments
.Where(a => a.StorageId == id && a.TimeStamp >= start)
.Select(a => new StorageUpdateModel
{
TimeStamp = a.TimeStamp,
Change = a.Volume,
Product = a.Product.Name,
UpdateType = StorageUpdateType.Adjustment,
Notes = "",
})
.Union(DbContext.StorageTrucks
.Where(t => t.StorageId == id && t.TimeStamp >= start)
.Select(t => new StorageUpdateModel
{
TimeStamp = t.TimeStamp,
Change = t.Volume,
Product = ""
UpdateType = StorageUpdateType.Truck,
Notes = "BOL ",
}))
.ToListAsync();
I can't see where columns have different store types. 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. |
Looks like Been a long and frustrating day. The following raw SQL does exactly what I need. It's bad enough that I need to go to raw SQL, but I can't even do this because SELECT sa.[TimeStamp],
sa.[Volume] AS [Change],
p.[Name] AS [Product],
1 AS [UpdateType],
sa.[UserName] AS [UpdateData],
sa.[Comments] AS [UpdateData2]
FROM [StorageAdjustments] sa
JOIN [Products] p ON sa.[ProductId] = p.[Id]
WHERE [StorageId] = @storageId and [TimeStamp] >= @startDate
UNION ALL
SELECT st.[TimeStamp],
st.[Volume] AS [Change],
NULL AS [Product],
2 AS [UpdateType],
CONVERT(NVARCHAR, t.[BillOfLading]) AS [UpdateData],
CONVERT(NVARCHAR, t.[ManualEntry]) AS [UpdateData2]
FROM [StorageTrucks] st
JOIN [Trucks] t ON st.[TruckId] = t.[Id]
WHERE [StorageId] = @storageId and [TimeStamp] >= @startDate
UNION ALL
SELECT sr.[TimeStamp],
sr.[Volume] AS [Change],
NULL AS [Product],
3 AS [UpdateType],
r.[RailcarNumber] AS [UpdateData],
NULL AS [UpdateData2]
FROM [StorageRailcars] sr
JOIN [Railcars] r ON sr.[RailcarId] = r.[Id]
WHERE [StorageId] = @storageId and [TimeStamp] >= @startDate |
Can you elaborate on this? Where are you seeing memory concerns?
Logging should make it easy to know exactly which SQL is being generated by EF.
ExecuteSqlRaw is for executing non-queries: are you aware of FromSqlRaw? (though I still think you should be able to use the Convert.ToString() workaround and keep using LINQ rather than raw SQL) |
@roji The discussion about potential problems with I am familiar with |
Duplicate of #19129 |
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 #19129 (comment)). |
SQL has a
UNION
operator. So I'm not sure I understand whyUnion()
doesn't appear to work.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)
The text was updated successfully, but these errors were encountered: