Skip to content
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

Query: Adds translation support for single key single value select GROUP BY LINQ queries #4074

Merged

Conversation

leminh98
Copy link
Contributor

@leminh98 leminh98 commented Sep 7, 2023

Pull Request Template

Description

This is the first of a set of PRs to add incremental support for Group By for LINQ queries. Specifically, this PR adds support for Group By queries grouping by a single key, and projecting a single value.

While LINQ has multiple function signatures for GroupBy queries, we will only currently support the current signature:
GroupBy<TSource,TKey,TResult>(IEnumerable, Func<TSource,TKey>, Func<TKey,IEnumerable,TResult>)

GroupBy(<key-selector lambda>, <value-selector lambda>)

Example of use:

query.GroupBy(k => k.Id, (key, values) => key)
 // translate to SELECT VALUE root["id"] FROM root GROUP BY root["id"]

query.GroupBy(k => k.Id, (key, values) => values.Min(value => value.age))
// translate to SELECT VALUE MIN(root["age"]) FROM root GROUP BY root["id"]

Limitations:

  • The value selector only accept Parameter and Aggregate Scalar expression
  • The GROUP BY query cannot be followed by other method call

These limitations are temporary and will change as more GROUP BY functionality will be supported.

Type of change

Please delete options that are not relevant.

  • [] Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • [] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [] This change requires a documentation update

Closing issues

To automatically close an issue: closes #IssueNumber

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good!

@leminh98 leminh98 changed the title LINQ GROUP BY translation support [Internal] Query: Adds translation support for single key single value select GROUP BY Oct 13, 2023
adityasa
adityasa previously approved these changes Feb 16, 2024
Copy link
Contributor

@adityasa adityasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

sboshra
sboshra previously approved these changes Mar 6, 2024
Copy link
Contributor

@sboshra sboshra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@leminh98 leminh98 added QUERY auto-merge Enables automation to merge PRs labels Apr 1, 2024
Copy link
Contributor

@adityasa adityasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@microsoft-github-policy-service microsoft-github-policy-service bot merged commit 15d83a7 into master Apr 1, 2024
21 checks passed
@microsoft-github-policy-service microsoft-github-policy-service bot deleted the users/leminh/LINQGroupBySupport branch April 1, 2024 19:18
@DM-98
Copy link

DM-98 commented Apr 5, 2024

New here; I'm just confused, got a question: Is this live on nuget package Microsoft.EntityFrameworkCore.Cosmos right now on version 8.0.3?

Getting error on 8.0.3:

Unhandled exception rendering component: The LINQ expression 'DbSet<PropertyView>()
          .GroupBy(
              keySelector: p => p.PropertyId,
              resultSelector: (key, g) => new PropertyWithReadCountDTO{
                  PropertyId = key,
                  PropertyTotalViews = g
                      .AsQueryable()
                      .Count(e => (int)e.EventType == 1),
                  PropertyTotalClicks = g
                      .AsQueryable()
                      .Count(e => (int)e.EventType == 0)
              }
          )' could not be translated...

The code:

List<PropertyWithReadCountDTO> result = await query.GroupBy(
	x => x.PropertyId,
	(key, values) => new PropertyWithReadCountDTO
	{
		PropertyId = key,
		PropertyTotalViews = values.Count(v => v.EventType == EventType.View),
		PropertyTotalClicks = values.Count(v => v.EventType == EventType.Click)
	}).ToListAsync(cancellationToken).ConfigureAwait(false);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-merge Enables automation to merge PRs QUERY
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants