[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:

var query = collection.AsQueryable().Select(c => c.Name).AsEnumerable().OrderBy(n => n);

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:

var query = collection.AsQueryable<X>().OrderBy(x => x.Name).Select(x => x.Name);

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
Message=Value cannot be null.
Parameter name: name
Source=MongoDB.Bson
ParamName=name
StackTrace:
at MongoDB.Bson.BsonDocument.Add(String name, BsonValue value) in C:\mongodb-mongo-csharp-driver-be03282\Bson\ObjectModel\BsonDocument.cs:line 626
at MongoDB.Driver.Linq.SelectQuery.Execute() in C:\mongodb-mongo-csharp-driver-be03282\Driver\Linq\Translators\SelectQuery.cs:line 150
at MongoDB.Driver.Linq.MongoQueryProvider.Execute(Expression expression) in C:\mongodb-mongo-csharp-driver-be03282\Driver\Linq\MongoQueryProvider.cs:line 146
at MongoDB.Driver.Linq.MongoQueryable`1.GetEnumerator() in C:\mongodb-mongo-csharp-driver-be03282\Driver\Linq\MongoQueryable.cs:line 81
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

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