[CSHARP-4762] Select with lambda parameter that is a base type of collection document type throws Created: 18/Aug/23  Updated: 10/Oct/23

Status: Backlog
Project: C# Driver
Component/s: None
Affects Version/s: 2.21.0
Fix Version/s: None

Type: Bug Priority: Blocker - P1
Reporter: Dimitri Kroo Assignee: Robert Stam
Resolution: Unresolved Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File TestProject2.7z    

 Description   

Hi,

 

using expression variables like below doesn't work with Linq V3

       Expression<Func<ParentBaseClass, ParentBaseClass>> select = x =>
new ParentBaseClass()

{     Id = x.Id, }

;

collection.AsQueryable().Select(select).ToList();

 

Best regards

Dimitri



 Comments   
Comment by Robert Stam [ 04/Oct/23 ]

Minimal repro:

var collection = GetCollection(linqProvider);
var queryable = collection.AsQueryable()
    .Select((B x) => x.X); // note: collection documents are of type C, but lambda parameter is of type B 

where B and C are declared as:

private class B
{
    public int Id { get; set; }
    public int X { get; set; }
}
 
private class C : B
{
}

Comment by Dimitri Kroo [ 02/Oct/23 ]

robert@mongodb.com 

Thank you for workaround!

 

 

 

 

Comment by Robert Stam [ 30/Sep/23 ]

I think I am able to reproduce your issue. The exception I get is:

 

System.ArgumentException : ValueType 'MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira.CSharp4762Tests+ParentClass' of parameterSerializer does not match parameter type 'MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira.CSharp4762Tests+ParentBaseClass'.
Parameter name: parameterSerializer
 

Is that the same exception you get?

I don't think the issue is related to CSHARP-4731.

In this case your `select` variable type does not match the collection type (at least not exactly).

If you declare your `select` variable instead as:

Expression<Func<ParentClass, ParentBaseClass>> selector1 = x => new ParentBaseClass { Id = x.Id }; 

the code works. I understand that your code as you provided should work, I'm only offering the above as a workaround.

When the `select` variable is of type `Expression<Func<ParentBaseClass, ParentBaseClass>>` there is an implicit conversion of `IMongoQueryable<ParentClass>` to `IQueryable<ParentBaseClass>` when `Select` is called, and that's what's confusing the LINQ3 translator.

 

Comment by Shiya Kohn [ 24/Aug/23 ]

robert@mongodb.com it seems that this is related to CSHARP-4731  (setting child classes to their parent type), currently it's only fixed for enumerables. The reporter typed the select expression with the base class `Func<Base, Base>` and v3 only supports `Func<Implementation, Base>` on `IMongoCollection<Implmentation>`

Comment by PM Bot [ 18/Aug/23 ]

Hi dimitri.kroo@baramundi.com, thank you for reporting this issue! The team will look into it and get back to you soon.

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