[CSHARP-4047] Nested where clause in select only works with IEnumerable<T> Created: 09/Feb/22  Updated: 28/Oct/23  Resolved: 24/Mar/22

Status: Closed
Project: C# Driver
Component/s: LINQ3
Affects Version/s: 2.14.0
Fix Version/s: 2.15.1

Type: Bug Priority: Unknown
Reporter: James Kovacs Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: triaged
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Epic Link: CSHARP-3615

 Description   

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')



 Comments   
Comment by Githook User [ 04/May/22 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4047: Nested where clause in select only works with IEnumerable<T>.
Branch: v2.15.x
https://github.com/mongodb/mongo-csharp-driver/commit/e235279db24f001b629e06a73b75a5321dcdd86a

Comment by Githook User [ 24/Mar/22 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4047: Nested where clause in select only works with IEnumerable<T>.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/8fcad7e4f13657d148ba6102e17af012b5f2ad8f

Comment by James Kovacs [ 10/Feb/22 ]

Note that the above code works in LINQ2. This is a new LINQ3-specific bug.

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