-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
EF Core 2.1 group by with where condition evaluated locally #12255
Comments
@smitpatel to add workaround. |
@mojtabakaviani - The query posted above does not compile in C#. It seems that you are trying to GroupBy an entity type. What are your expectations in terms of how it would translate to server GROUP BY? |
If the expectation is to find stock for all books per library then following query could help var q = (from l in db.Set<Library>()
join s in db.Books.GroupBy(b => b.LibraryId, b => b.Enter - b.Exit)
.Where(g => g.Sum() > 0)
.Select(g => new
{
g.Key,
Stock = g.Sum()
})
on l.Id equals s.Key
select new
{
LibraryName = l.Name,
s.Stock
}).ToList(); Produces SQL SELECT [t].[Key], [t].[Stock], [l].[Name] AS [LibraryName]
FROM [Library] AS [l]
INNER JOIN (
SELECT [b].[LibraryId] AS [Key], SUM([b].[Enter] - [b].[Exit]) AS [Stock]
FROM [Books] AS [b]
GROUP BY [b].[LibraryId]
HAVING SUM([b].[Enter] - [b].[Exit]) > 0
) AS [t] ON [l].[Id] = [t].[Key] |
Putting this on the backlog because the translation seems desirable, while recognizing that the cost of doing so may ultimately be prohibitive. |
@smitpatel your query maybe work but is very complex. |
I am also having this problem when I am using conditional count:
|
@qwertyuiope - The feature you are looking for is #11711 |
Hi guys. What about the case with Ranking? Let's say I have a table with clients. And I need to group them by Country and select the top1 earning client in each country. So result should be: I use This query will be evaluated locally. Do you have any advises to execute it on server? EF 6.2 can do it. |
@smitpatel Will this be supported (as in EF 6 for example) or should the queries be rewritten? This seems like a problem when migrating projects. |
@Stamo-Gochev - This issue is in backlog milestone. We plan to implement at some point but it is not currently planned for next release. |
EF have many group by issues but i thinking not in man road map and EF team work in your ideas not cutomers feadbacks. Many issues that customers requested in backlog milestone for long times. |
I'am trying to use Group by to remove duplicates, calculated by multiple columns.
Both evaluating locally. |
EF Core 2.2 A simple group by in LINQ to SQL evaluates locally, and also throws a
from tags in _context.Tags
from fileTag in _context.FileTags
.Where(x => x.TagId == tags.Id)
.DefaultIfEmpty()
from packageTag in _context.FileTags
.Where(x => x.TagId == tags.Id)
.DefaultIfEmpty()
group tags by tags.Id into tagIds
select new
{
Id = tagIds.Key,
Count = tagIds.Count()
}; Generated Queries: Query1: SELECT "tags0"."Id"
FROM "Tags" AS "tags0" Query2: SELECT 1
FROM (
SELECT NULL AS "empty"
) AS "empty4"
LEFT JOIN (
SELECT "x4".*
FROM "FileTags" AS "x4"
WHERE "x4"."TagId" = @_outer_Id2
) AS "t4" ON 1 = 1
Expected Query: I'd expect along the lines of: SELECT
Tags.Id,
Count(Tags.Id) as Count
FROM
Tags
LEFT JOIN FileTags ON Tags.Id = FileTags.TagId
LEFT JOIN PackageTags on Tags.Id = PackageTags.TagId
GROUP BY Tags.Id This and other queries are being used on test data, but local evaluation of millions of records is a hard stop anyways... |
Duplicate of #11711 |
Warning
Further technical details
EF Core version: 2.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10 x64
IDE: Visual Studio 2017 15.8 Preview 2
The text was updated successfully, but these errors were encountered: