Skip to content

Conversation

leminh98
Copy link
Contributor

@leminh98 leminh98 commented Oct 28, 2024

Pull Request Template

Description

This PR features the new syntax for multi-key group by queries, allowing for grouping of multiple properties.

Full syntax:

// Function Signature
public static System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TElement>> GroupBy<TSource,TKey,TElement> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector);

// Usage
Enumerables.GroupBy( groupByKeys => keySelectiorLambda, (keys, values) => valueSelectorLambda);

Example

  • Grouping by single keys and projecting single value
Enumerable.GroupBy( /*keySelector*/ k => k.Id,  /*valueSelector*/ (key, values) => values.Count());
  • Grouping by single keys and projecting multiple values
Enumerable.GroupBy( 
              /*keySelector*/ k => k.Id, 
              /*valueSelector*/ (key, values) => new
               {
                   idField = key.key1,
                   count = values.Count(),
               });
  • Grouping by multiple keys and projecting single value
Enumerable.GroupBy( 
              /*keySelector*/  k => new
                 {
                     key1 = k.Id,
                     key2 = k.Int
                 } , 
              /*valueSelector*/ (key, values) => values.Count());
  • Grouping by multiple keys and projecting multiple values
Enumerable.GroupBy(
/*keySelector*/
              k => new
                  {
                      key1 = k.Id,
                      key2 = k.Int
                  } , 
/*valueSelector*/
                (key, values) => new
                {
                    idField = key.key1,
                    count = values.Count(),
                })

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

closes ##1202

@adityasa
Copy link
Contributor

adityasa commented Nov 22, 2024

        bool ignoreOrderingForAnonymousTypeObject = false,

Please rename to just ignoreOrder #Closed


Refers to: Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/Linq/LinqTestsCommon.cs:686 in 7c8ff08. [](commit_id = 7c8ff08, deletion_comment = False)

@leminh98 leminh98 added the auto-merge Enables automation to merge PRs label Nov 22, 2024
adityasa
adityasa previously approved these changes Nov 24, 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:

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 24b8ca6 into master Nov 25, 2024
24 checks passed
@microsoft-github-policy-service microsoft-github-policy-service bot deleted the users/leminh/groupbyLinqMultiKey branch November 25, 2024 02:35
@benjaminmillhouse
Copy link

Hello,
This feature is working great - however, I've noticed that we cannot use a concrete class in the Projection portion of the call... (valueSelector). If we try to use one instead of an anonymous object, we receive the error: Microsoft.Azure.Cosmos.Linq.DocumentQueryException: Expression with NodeType 'MemberInit' is not supported.

I'm just curious if that is expected behavior? I have been able to use concrete classes over anonymous objects in .Select() operations before, this seems specific to the new GroupBy functionality.

Thanks!

@sacgaikwad
Copy link

sacgaikwad commented Dec 12, 2024

hello folks,

I was looking for multikey grouping, however when I pass the multiple keys it give error:
Microsoft.Azure.Cosmos.Linq.DocumentQueryException: 'Only path expressions are supported for SelectMany.'

Method signature:
public async Task<IEnumerable> QueryAsync<T, TKey, TResult>(Expression<Func<T, bool>> filter, Expression<Func<T, TKey>> keySelector, Expression<Func<TKey, IEnumerable, TResult>> resultSelector, CancellationToken cancellationToken = default)

Exception at :
var query = container.GetItemLinqQueryable().Where(filter).GroupBy(keySelector, resultSelector).ToFeedIterator();

input:
Expression<Func<ApiMetrics, dynamic>> keySelector = query =>
new
{
key1 = query.CompanyId,
key2 = query.ProductType
};

Can someone help here?

@Amit-BC
Copy link

Amit-BC commented Jan 26, 2025

hello folks,

I was looking for multikey grouping, however when I pass the multiple keys it give error: Microsoft.Azure.Cosmos.Linq.DocumentQueryException: 'Only path expressions are supported for SelectMany.'

Method signature: public async Task QueryAsync<T, TKey, TResult>(Expression<Func<T, bool>> filter, Expression<Func<T, TKey>> keySelector, Expression<Func<TKey, IEnumerable, TResult>> resultSelector, CancellationToken cancellationToken = default)

Exception at : var query = container.GetItemLinqQueryable().Where(filter).GroupBy(keySelector, resultSelector).ToFeedIterator();

input: Expression<Func<ApiMetrics, dynamic>> keySelector = query => new { key1 = query.CompanyId, key2 = query.ProductType };

Can someone help here?

is there a solution for this? I get the same error

@Maya-Painter
Copy link
Contributor

@benjaminmillhouse @sacgaikwad @Amit-BC If you are still receiving these errors in version 3.47.0 please open a new issue so we can investigate further.

@benjaminmillhouse
Copy link

@benjaminmillhouse @sacgaikwad @Amit-BC If you are still receiving these errors in version 3.47.0 please open a new issue so we can investigate further.

The issue I reported seems to be resolved now. Thanks!

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants