[CSHARP-1698] Method groups Created: 29/Jun/16  Updated: 17/Nov/22

Status: Backlog
Project: C# Driver
Component/s: Linq, LINQ3
Affects Version/s: 2.2.4
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Chris Robison Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: triaged
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

We have the following query, sequencesNumbers is an IEnumerable<int>:

Collection.Find(i => i.Profiles.Any(sequenceNumbers.Contains))

This returns the following exception:

Unable to cast object of type 'System.Linq.Expressions.ConstantExpression' to type 'System.Linq.Expressions.LambdaExpression'. 
    ExceptionSourceMethod:  System.Linq.Expressions.Expression Bind(MongoDB.Driver.Linq.Expressions.PipelineExpression, MongoDB.Driver.Linq.Processors.EmbeddedPipeline.EmbeddedPipelineBindingContext, System.Linq.Expressions.MethodCallExpression, System.Collections.Generic.IEnumerable`1[System.Linq.Expressions.Expression]) 
    ExceptionStackTrace:     at MongoDB.Driver.Linq.Processors.EmbeddedPipeline.MethodCallBinders.AnyBinder.Bind(PipelineExpression pipeline, EmbeddedPipelineBindingContext bindingContext, MethodCallExpression node, IEnumerable`1 arguments)
   at MongoDB.Driver.Linq.Processors.MethodInfoMethodCallBinder`1.Bind(PipelineExpression pipeline, TBindingContext bindingContext, MethodCallExpression node, IEnumerable`1 arguments)
   at MongoDB.Driver.Linq.Processors.CompositeMethodCallBinder`1.Bind(PipelineExpression pipeline, TBindingContext bindingContext, MethodCallExpression node, IEnumerable`1 arguments)
   at MongoDB.Driver.Linq.Processors.PipelineBinderBase`1.BindMethodCall(MethodCallExpression node)
   at MongoDB.Driver.Linq.Processors.EmbeddedPipeline.EmbeddedPipelineBinder.Bind(Expression node, IBindingContext parent)
   at MongoDB.Driver.Linq.Processors.SerializationBinder.Visit(Expression node)
   at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate[TDocument](Expression`1 predicate, IBsonSerializer`1 parameterSerializer, IBsonSerializerRegistry serializerRegistry)
   at MongoDB.Driver.MongoCollectionImpl`1.CreateFindOperation[TProjection](FilterDefinition`1 filter, FindOptions`2 options)
   at MongoDB.Driver.MongoCollectionImpl`1.FindSync[TProjection](FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.AsyncCursorSourceEnumerableAdapter`1.GetEnumerator()

Following works however:

Collection.Find(i => i.Profiles.Any(p => sequenceNumbers.Contains(p)))

The only reason we ran into this is that Resharper wanted to convert the lambda into a method group. This is not a big deal, but it might be good to add support for these kinds of optimizations.


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