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

Nested where clause in select only works with IEnumerable<T>

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 2.15.1
    • Affects Version/s: 2.14.0
    • Component/s: LINQ3

      If you define a nested collection as IEnumerable<T>, then you can project out elements via Select. If you define it as T[], IList<T>, or any other collection type, LINQ3 is not able to serialize it to MQL.

      using System.Collections.Generic;
      using MongoDB.Driver;
      using MongoDB.Driver.Linq;
      
      var settings = new MongoClientSettings { LinqProvider = LinqProvider.V3 };
      var client = new MongoClient(settings);
      var db = client.GetDatabase("test");
      var coll = db.GetCollection<Foo>("foo");
      
      var query = coll.AsQueryable().Select(x => new { MatchingBars = x.Bars.Where(y => y.Text == "foo") });
      Console.WriteLine(query);
      
      class Foo
      {
          public Bar[] Bars { get; set; } // <-- IEnumerable<Bar> works, but other collection types do not
      }
      
      class Bar
      {
          public string Text { get; set; }
      }
      

      The code throws the following exception:

      Value type of serializer is Bar[] and does not match member type System.Collections.Generic.IEnumerable`1[[Bar, csharp4047, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. (Parameter 'serializer')
      

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            james.kovacs@mongodb.com James Kovacs
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: