[CSHARP-1772] Quering for first item of GroupBy aggregation produces NotSupportedException Created: 29/Sep/16  Updated: 04/Feb/22  Resolved: 04/Feb/22

Status: Closed
Project: C# Driver
Component/s: Linq, LINQ3
Affects Version/s: 2.3
Fix Version/s: 2.14.0

Type: Bug Priority: Major - P3
Reporter: Oleg Osovitskiy Assignee: James Kovacs
Resolution: Done Votes: 2
Labels: rp-track, triaged
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to CSHARP-2071 Using Linq Aggregation to project the... Closed
Epic Link: CSHARP-3615

 Description   

I have entities with compound ids in my collection: {_id: {_id: guid, version: int}}. When I need to query for latest versions of objects with some guid identificators i use following LINQ query:

Collection.AsQueryable()
    .Where(x => ids.Contains(x.Id.Id))
    .OrderByDescending(x => x.Id.Version)
    .GroupBy(x => x.Id.Id)
    .Select(x => x.First())
    .Select(x => new
    {
        Id = x.Id.Id,
        Title = x.Title
    }).ToList();

With 2.3 driver version it works when I make projection in the end. But when I need to extract whole object it fails with NotSupportedException:

Collection.AsQueryable()
    .Where(x => newIds.Contains(x.Id.Id))
    .OrderByDescending(x => x.Id.Version)
    .GroupBy(x => x.Id.Id)
    .Select(x => x.First)
    .ToList();

I tried to workaround this with "fake" projection without success either:

Collection.AsQueryable()
    .Where(x => newIds.Contains(x.Id.Id))
    .OrderByDescending(x => x.Id.Version)
    .GroupBy(x => x.Id.Id)
    .Select(x => new
    {
        Entity = x.First()
    })
    .AsEnumerable()
    .Select(x => x.Entity)
    .ToList();

Any suggestions?



 Comments   
Comment by James Kovacs [ 04/Feb/22 ]

This issue has been fixed in the new LINQ provider (known as LINQ3), which is included in the 2.14 release.

Configure your MongoClientSettings to use LinqProvider.V3 if you want to use this functionality.

To configure a client to use the LINQ3 provider use code like the following

var connectionString = "mongodb://localhost";
var clientSettings = MongoClientSettings.FromConnectionString(connectionString);
clientSettings.LinqProvider = LinqProvider.V3;
var client = new MongoClient(clientSettings);

Comment by Shiya Kohn [ 20/Feb/20 ]

Hi Craig

 

This has STILL (4 years later) not been implemented, despite many issues being opened about this.

Comment by Craig Wilson [ 02/Nov/16 ]

Hi Oleg,

Sorry for the late reply. I believe the issue is that you are attempting to select the entire root object, as opposed to part of it. We don't currently have support for this.

I believe the fix would require us to use the $$ROOT variable to make this work.

Craig

Generated at Wed Feb 07 21:40:39 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.