[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: |
|
||||
| Epic Link: | CSHARP-3615 | ||||
| Description |
|
I have a code like this in my ASP.NET Core app
and it throws the following exception when executed:
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
| ||||
| 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. |