title: C# - Unable to cast object of type 'System.Linq.Expressions.ConstantExpression' to type 'System.Linq.Expressions.LambdaExpression'
body: <p>I was trying to retrieving data from a collection as follow</p>
  
  <pre><code>var results = await repository.FindAsync(GetFilter(ids));
  
  private System.Linq.Expressions.Expression&lt;Func&lt;SomeEntity, bool&gt;&gt; GetFilter(IEnumerable&lt;long&gt; someIds)
  {
      return x =&gt; someIds.Contains(x.Id) &amp;&amp;
                  x.subscriptions
                 .Where(s =&gt; s.servicePlans.Contains((int)ServicePlanEnum.Service1)).Any();
  }
  </code></pre>
  
  <p>but I got the following exception </p>
  
  <blockquote>
    <p>System.InvalidCastException: Unable to cast object of type 'System.Linq.Expressions.ConstantExpression' to type 'System.Linq.Expressions.LambdaExpression'.</p>
  </blockquote>
  
  <p>Where am I wrong?</p>
  
  <p>EDIT:</p>
  
  <p>Subscription object looks like:</p>
  
  <pre><code>subscription :
  {
      prop1 : string,
      prop2 : int,
      servicePlans : int[]
  }
  </code></pre>
  
  <p>FindAsync definition:</p>
  
  <pre><code>protected IMongoCollection&lt;TEntity&gt; Collection =&gt; database.GetCollection&lt;TEntity&gt;(collectionName);
  private IFindFluent&lt;TEntity, TEntity&gt; Find(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate) =&gt; Collection.Find(predicate);
  public virtual async Task&lt;ICollection&lt;TEntity&gt;&gt; FindAsync(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate, SortOrder&lt;TEntity&gt;[] sort = null, int? page = null, int? limit = null)
  {
      var query = Find(predicate);
  
      return await
          query
              .Skip(page == null &amp;&amp; limit == null ? null : (page - 1) * limit)
              .Limit(limit)
              .Sort(sort)
              .ToListAsync();
  }
  </code></pre>
  
  <p>The error is caused by the following condition</p>
  
  <pre><code>x.subscriptions.Where(s =&gt; s.servicePlans.Contains((int)ServicePlanEnum.Service1)).Any();
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>stacktrace:</p>
  
  <blockquote>
    <p>Unable to cast object of type 'System.Linq.Expressions.ConstantExpression' to type 'System.Linq.Expressions.LambdaExpression'. System.InvalidCastException: Unable to cast object of type 'System.Linq.Expressions.ConstantExpression' to type 'System.Linq.Expressions.LambdaExpression'.
       at MongoDB.Driver.Linq.ExpressionHelper.GetLambda(Expression node)
       at MongoDB.Driver.Linq.Processors.EmbeddedPipeline.MethodCallBinders.AnyBinder.Bind(PipelineExpression pipeline, EmbeddedPipelineBindingContext bindingContext, MethodCallExpression node, IEnumerable<code>1 arguments)
       at MongoDB.Driver.Linq.Processors.MethodInfoMethodCallBinder</code>1.Bind(PipelineExpression pipeline, TBindingContext bindingContext, MethodCallExpression node, IEnumerable<code>1 arguments)
       at MongoDB.Driver.Linq.Processors.CompositeMethodCallBinder</code>1.Bind(PipelineExpression pipeline, TBindingContext bindingContext, MethodCallExpression node, IEnumerable<code>1 arguments)
       at MongoDB.Driver.Linq.Processors.PipelineBinderBase</code>1.BindMethodCall(MethodCallExpression node)
       at MongoDB.Driver.Linq.Processors.EmbeddedPipeline.EmbeddedPipelineBinder.Bind(Expression node, IBindingContext parent)
       at MongoDB.Driver.Linq.Processors.SerializationBinder.VisitMethodCall(MethodCallExpression node)
       at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
       at MongoDB.Driver.Linq.Processors.SerializationBinder.Visit(Expression node)
       at System.Linq.Expressions.ExpressionVisitor.VisitBinary(BinaryExpression node)
       at MongoDB.Driver.Linq.Processors.SerializationBinder.VisitBinary(BinaryExpression node)
       at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)
       at MongoDB.Driver.Linq.Processors.SerializationBinder.Visit(Expression node)
       at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate[TDocument](Expression<code>1 predicate, IBsonSerializer</code>1 parameterSerializer, IBsonSerializerRegistry serializerRegistry)
       at MongoDB.Driver.MongoCollectionImpl<code>1.CreateFindOperation[TProjection](FilterDefinition</code>1 filter, FindOptions<code>2 options)
       at MongoDB.Driver.MongoCollectionImpl</code>1.FindAsync[TProjection](IClientSessionHandle session, FilterDefinition<code>1 filter, FindOptions</code>2 options, CancellationToken cancellationToken)
       at MongoDB.Driver.MongoCollectionImpl<code>1.&lt;&gt;c__DisplayClass43_0</code>1.b__0(IClientSessionHandle session)
       at MongoDB.Driver.MongoCollectionImpl<code>1.UsingImplicitSessionAsync[TResult](Func</code>2 funcAsync, CancellationToken cancellationToken)
       at MongoDB.Driver.IAsyncCursorSourceExtensions.ToListAsync[TDocument](IAsyncCursorSource<code>1 source, CancellationToken cancellationToken)
       at ...Repository.MongoRepository</code>2.FindAsync(Expression<code>1 predicate, SortOrder</code>1[] sort, Nullable<code>1 page, Nullable</code>1 limit) in ...\Infastructure\Repositories\MongoRepository.cs:line 71
       at ...Controllers.AccountController.GetCliniciansByClinicAsync(String clinicId, CancellationToken cancellationToken) in ...\Controllers\AccountController.cs:line 94
       at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
       at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextExceptionFilterAsync()</p>
  </blockquote>