[CSHARP-2980] InvalidOperationException when using Distinct in LINQ Created: 26/Feb/20  Updated: 28/Oct/23  Resolved: 15/Feb/22

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

Type: Bug Priority: Major - P3
Reporter: Serghei Sarafudinov Assignee: James Kovacs
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
Epic Link: CSHARP-3615

 Description   

I have a code like this in my ASP.NET Core app

var evtQry = from pe in EventDocCollection.AsQueryable()
                    where pe.Create.User.Id == user.Id
                    orderby pe.DateTime descending
                    select pe.Id;
var eventIds = await evtQry.Distinct().Take(20).ToListAsync();

and it throws the following exception when executed:

System.InvalidOperationException: Extension node must override the property Expression.Type.
   at System.Linq.Expressions.Expression.get_Type()
   at MongoDB.Driver.Linq.Expressions.GroupByExpression..ctor(Expression source, Expression keySelector)
   at MongoDB.Driver.Linq.Processors.Pipeline.MethodCallBinders.DistinctBinder.Bind(PipelineExpression pipeline, PipelineBindingContext bindingContext, MethodCallExpression node, IEnumerable`1 arguments)
   at MongoDB.Driver.Linq.Processors.MethodInfoMethodCallBinder`1.Bind(PipelineExpression pipeline, TBindingContext bindingContext, MethodCallExpression node, IEnumerable`1 arguments)
   at MongoDB.Driver.Linq.Processors.PipelineBinderBase`1.BindMethodCall(MethodCallExpression node)

Do you have any idea why this exception is thrown? It looks like a problem with the C# driver. 



 Comments   
Comment by James Kovacs [ 15/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 James Kovacs [ 06/Oct/20 ]

Thank you for reproducing and reporting this issue in the MongoDB .NET Driver. We were able to reproduce this issue in 2.10.0 as well as the latest build (2.11.2). This is an edge case in our current LINQ implementation whereby calling orderby followed by Distinct is not supported.

One potential workaround is to reverse the order of Distinct and ToList so that the Distinct operation is performed by LINQ-to-Objects. This would require all non-distinct results to be read into memory. If this is not a practical solution, you can express "distinct" as a group by operation within the LINQ query itself.

We are currently working on improvements to our LINQ implementation and will take this edge case into consideration. Thank you again for reporting this issue.

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