Closed arthur-liberman closed 2 years ago
This seems to be the best I can do right now.
public IList<Dto> ExecuteQuery()
{
using (var dbContext = new MyDbContext())
{
var grouping = (from i in dbContext.Items
join tmp_g in dbContext.Groups on i.GroupId equals tmp_g.Id into gj_g
from g in gj_g.DefaultIfEmpty()
join tmp_c in dbContext.Categories on i.CategoryId equals tmp_c.Id into gj_c
from c in gj_c.DefaultIfEmpty()
select new { i, g, c }).ToList();
var result = from g in grouping
group new { gName = g.g.Name, cName = g.c.Name } by new { g.i.Id, g.i.Name } into g2
select new Dto
{
Id = g2.Key.Id,
Name = g2.Key.Name,
GroupCount = g2.Select(a => a.gName).Distinct().Count(),
CategoryCount = g2.Select(a => a.cName).Count()
};
return result.ToList();
}
}
SELECT [i].[id], [i].[categoryId], [i].[groupId], [i].[name], [g].[id], [g].[name], [c].[id], [c].[name]
FROM [Item] AS [i]
LEFT JOIN [Group] AS [g] ON [i].[groupId] = [g].[id]
LEFT JOIN [Category] AS [c] ON [i].[categoryId] = [c].[id]
Duplicate of #17376
@smichtch I've searched, but couldn't find any dups.
What about the Distinct()
issue I mentioned? Is it the same as Select()
?
@arthur-liberman #17376 is the duplicate.
@ajcvickers The error seems to be different, but the situation described in that issue is the same.
Only in my case g2.Select(a => a.CatName).Count()
fails too. There seem to be 2 issues here. With the key selector and with the Distinct call.
g2.Select(a => a.CatName).Count()
is same as g2.Count()
so we don't support Select when doing Count.
Then perhaps I'm doing something wrong. The idea is to get the SQL query I posted in the OP.
SELECT I.id, I.name, COUNT(C.name) AS CatCount, COUNT(DISTINCT G.name) AS GroupCount
FROM [Item] I
LEFT OUTER JOIN [Group] G ON I.groupId = G.id
LEFT OUTER JOIN [Category] C ON I.categoryId = C.id
GROUP BY I.id, I.name
If you seed the DB using the code sample in OP, and run the query above on the DB directly, you will see that each count column will contain different results.
Maybe the way I use count is wrong? Am I supposed to use g2.Count(a => a.CatName != null)
?
Yes, but that would also be duplicate of #11711
To be completely frank, I don't think I follow.
I understand that I can't use Distinct()
right now, and it's a known issue.
But I still don't fully understand what would be the correct LINQ in order to achieve a matching translation to the original SQL query if both #11711 and #17376 were fixed.
I am porting SQL queries in our product to Linq queries to be used with EF. We have an SQL query similar to this:
Executing this on the DB directly works as expected.
The following query in Linq
generates the following exception:
I checked on 3.1 and latest 5.0/master with identical results. If I remove
.Select(a => a.GroupName)
and.Select(a => a.CatName)
, the generated SQL is as follows (missing select forGroupCount
):In addition to the above, using
Distinct()
also results in a similar error that the query cannot be translated.Steps to reproduce
Further technical details
EF Core version: 3.1.1 (SDK 3.1.101) Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: NET Core 3.1, 5.0 Operating system: Windows Server 2012 R2 IDE: Visual Studio 2019 16.4.2, Visual Studio Code