[CSHARP-852] System.ArgumentOutOfRangeException when attempting to use custom IComparer with LINQ OrderBy Created: 25/Oct/13 Updated: 05/Apr/19 Resolved: 25/Oct/13 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | None |
| Affects Version/s: | 1.8.3 |
| Fix Version/s: | None |
| Type: | Task | Priority: | Minor - P4 |
| Reporter: | Jon Swain | Assignee: | Unassigned |
| Resolution: | Done | Votes: | 0 |
| Labels: | question | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
windows 7 x64 |
||
| Attachments: |
|
| Description |
|
Hi, I've been getting an System.ArgumentOutOfRangeException when i attempt to use a custom IComparer with the OrderBy LINQ expression. var results = collection.AsQueryable<TestClass>().OrderBy( m => m.Id, new NaturalSortComparer<string>() ).ToList(); I've attached a program which demonstrates the problem. I do have a workaround for this by putting ToList before the OrderBy call but this is not ideal. Any suggestions? |
| Comments |
| Comment by Jon Swain [ 25/Oct/13 ] |
|
Some of our Ids are alpha numeric so we can't store as integers. My data sets are not that large so i can handle it client side. Cheers for your help |
| Comment by Craig Wilson [ 25/Oct/13 ] |
|
Yeah, that's just kinda how it is. LINQ is a big leaky abstraction so it's gonna let you do a lot of stuff client-side that isn't always possible server side. You'll get runtime exceptions when this happens. Keep in mind that a client-side sort is going to be done in memory, so if you have lots of data, it could be slow. Any reason not to store these as integers instead of strings? |
| Comment by Jon Swain [ 25/Oct/13 ] |
|
OK, i didn't realize this. The built in sort is ok but as they are string representations of numbers i don't get a natural sort. i.e. i get the following 1 but the NaturalSortComparer sort like the following 1 Its not a big issue i can workaround it or do the sorting on the client side. Cheers |
| Comment by Craig Wilson [ 25/Oct/13 ] |
|
You can't use a custom comparer for LINQ. To do this would mean we'd have to interpret the code in your custom comparer and then send that to MongoDB and it would need to apply that custom logic there. All of this is impossible. Unfortunately, the only way to accomplish this would be to do the sort client-side. Are you sure the built-in MongoDB sort is not accurate? What does your NaturalSortComparer do differently? |