Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-3144

Linq Contains produce wrong expression

    • Type: Icon: New Feature New Feature
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 2.19.0
    • Affects Version/s: 2.10.4
    • Component/s: Linq
    • Labels:
      None
    • Environment:
      .net core 3.1
    • Fully Compatible

      when i try to check if collection contains some element i get System.ArgumentException: Unsupported filter: Contains().

      for example, we have Order class with collection of order positions:

      class Order

      {    public int Id \{ get; set; }

         public List<OrderItem> Items { get; set; }

      ...

      }

       

      in each position we have GoodId and Amount:

      class OrderPosition
      {    public int Id { get; set; }

         public int GoodId { get; set; }

         public int Amount { get; set; }

      }

       

      now i want to select all Orders where some  good is present:

      var orders = db
         .GetCollection<Order>("order").AsQueryable()
         .Where(x => x.Items.Select(e => e.GoodId).Contains(2))
         .ToArray();

       

      yes, i know that i can rewrite query, but thissample is adopted my work case, so i nee dexactly that expression

       

      and there is i get exception:

      System.ArgumentException: Unsupported filter: Contains({document}

      {Items}

      .Select({document}

      {GoodId}

      )).
      at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node)
      at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry)
      at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateWhere(WhereExpression node)
      at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node)
      at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslatePipeline(PipelineExpression node)
      at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node)
      at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry, ExpressionTranslationOptions translationOptions)
      at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.Translate(Expression expression)
      at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.Execute(Expression expression)
      at MongoDB.Driver.Linq.MongoQueryableImpl`2.GetEnumerator()
      at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
      at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
      at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
      at ConsoleApp1.Program.Main(String[] args) in 

       

      now i use workaround and use following expression:

      var orders = db
         .GetCollection<Order>("order").AsQueryable()
         .Where(x => x.Items.Select(e => e.GoodId).Any(e => e == 2))
         .ToArray();

       

      but iu think that this bug need to be fixed

       

      i created fiddle with full example: https://dotnetfiddle.net/yfQk7U

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            anatoly.kryzhanovsky@singularis-lab.com Анатолий Крыжановский
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: