[CSHARP-1773] Sample operator throws ArgumentException Created: 03/Oct/16  Updated: 03/Nov/16  Resolved: 03/Nov/16

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: 2.3
Fix Version/s: 2.4

Type: Bug Priority: Major - P3
Reporter: Attila Kisko Assignee: Craig Wilson
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Minor Change

 Description   

An unhandled exception of type 'System.ArgumentException' occurred in System.Core.dll
 
Additional information: Expression of type 'System.Linq.IQueryable`1[T]' cannot be used for parameter of type 
  'MongoDB.Driver.Linq.IMongoQueryable`1[T]' of method 
  'MongoDB.Driver.Linq.IMongoQueryable`1[T] Sample[SubAgg]
  (MongoDB.Driver.Linq.IMongoQueryable`1[T], Int64)'

This is coming from a relatively simple query: collection.AsQueryable().Where().OrderBy().Sample();

The reason for the exception is that

1. tho the operations in MongoQueryable are defined as IMongoQueryable<T> Operation(this IMongoQueryable<T>,...)
2. They call into the original LINQ implementation (IMongoQueryable<TResult>)Queryable.Operation(source, ...)
3. Thus they return an Expression with the Type IQueryable<T> (source.Expression.Type)

Sample() creates the expression using Expression.Call, but its method signature does not "accept" the expression (as per the validations inside Expression.Call).

The solution is to change the method signature to

public static IMongoQueryable<TSource> Sample<TSource>(this IQueryable<TSource> source, long count)

Not sure if this solution is acceptable, but haven't found any better way (without reimplementing all LINQ operations so they have IMongoQueryable as their Expression.Type)

I have a pull request & a simple unit test which I can submit if you want it

https://github.com/enyim/mongo-csharp-driver/commit/0910aa62b26a7c4460fa83ed03cb87e52a89c971



 Comments   
Comment by Githook User [ 03/Nov/16 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1773: fix issue with using sample after another pipeline stage.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/18d204a046373bdceb32f2e17030d3ca8fa12d59

Comment by Craig Wilson [ 02/Nov/16 ]

Hi Attila,

Sorry for the late response. In regards to your question, we do "reimplement" every operator in MongoQueryable. For instance: where -> https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Driver/Linq/MongoQueryable.cs#L2810.

However, the issue was this line:
https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Driver/Linq/MongoQueryable.cs#L846.

It should have looked like this:
Expression.Convert(source.Expression, typeof(IMongoQueryable<TSource>)),

Will get it fixed.
Thanks for the report.
Craig

Comment by Attila Kisko [ 31/Oct/16 ]

anyone?

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