[CSHARP-492] LINQ - OrderBy after Select Doesn't Work Created: 13/Jun/12 Updated: 20/Mar/14 Resolved: 18/Jun/12 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | None |
| Affects Version/s: | 1.4.2 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Minor - P4 |
| Reporter: | Zaid Masud | Assignee: | Unassigned |
| Resolution: | Won't Fix | Votes: | 0 |
| Labels: | C#, LINQ, linq,query | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
The following type of query throws an ArgumentNull exception in BsonDocument.cs Add method for parameter string name (line 624): .Where().Select(x => x.Name).OrderBy(x => x).ToArray() |
| Comments |
| Comment by Craig Wilson [ 18/Jun/12 ] | |
|
Thanks for reporting Zaid. Because we will not be supporting this for standard queries, I'm going to close this report. | |
| Comment by Zaid Masud [ 13/Jun/12 ] | |
|
Makes sense... thanks for the responsiveness | |
| Comment by Robert Stam [ 13/Jun/12 ] | |
|
Either way would work. The difference is that ToList() and ToArray() cause the query to be evaluated immediately where AsEnumerable preserves the delayed execution behavior of LINQ queries. | |
| Comment by Zaid Masud [ 13/Jun/12 ] | |
|
Fair enough. Although .ToList() or .ToArray() are a more standard use than AsEnumerable() to say you want the rest of the query to be executed on client side. See http://stackoverflow.com/questions/3389855/am-i-misunderstanding-linq-to-sql-asenumerable | |
| Comment by Robert Stam [ 13/Jun/12 ] | |
|
I believe the standard LINQ way to say you want the rest of the LINQ query to be executed at the client is to stick a call to AsEnumerable() in the chain at the point where you want to transition from server side execution to client side execution:
| |
| Comment by Zaid Masud [ 13/Jun/12 ] | |
|
Great, yes sorry I suppose the real issue here is only the error message and I was able to figure out the solution pretty easily. As a side note I suppose there could be an argument that the OrderBy after a select should effectively be an "in-memory" sort as opposed to a Mongo server sort. | |
| Comment by Robert Stam [ 13/Jun/12 ] | |
|
We don't support OrderBy after Select (because the OrderBy clause has to be expressed in terms of the original document type of the collection to be mapped to an order by clause in the MongoDB wire protocol). You can rewrite the query this way:
I'm leaving the JIRA ticket open nonetheless because the error message you are getting is not the appropriate response. We will improve the error message. | |
| Comment by Zaid Masud [ 13/Jun/12 ] | |
|
System.ArgumentNullException was unhandled by user code |