[CSHARP-4304] Handle conversion to an interface Created: 30/Aug/22  Updated: 28/Oct/23  Resolved: 07/Oct/22

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 2.17.1
Fix Version/s: 2.18.0

Type: Bug Priority: Unknown
Reporter: Damith G Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible

 Description   

the following exception is being thrown by linq v3 if a simple join query is performed within a generic method:

> System.ArgumentException: 'Invalid toType: IEntity. (Parameter 'toType')'

here's how to reproduce it:

the classes

public interface IEntity
{
    string Id { get; set; }
}
 
public class Parent : IEntity
{
    [BsonId]
    public string Id { get; set; }
 
    [BsonRepresentation(BsonType.ObjectId)]
    public string ChildId { get; set; }
}
 
public class Child : IEntity
{
    [BsonId]
    public string Id { get; set; }
}

the program

var children = GetChildrenQueryable<Child>(db, "Child");
children.Count();
 
private IMongoQueryable<TChild> GetChildrenQueryable<TChild>(IMongoDatabase db, string childCollectionName) where TChild : IEntity
{
    var parentCollection = db.GetCollection<Parent>("Parent");
    var childCollection = db.GetCollection<TChild>(childCollectionName).AsQueryable();
 
    return parentCollection
        .AsQueryable()
        .Join(
            childCollection,
            p => p.ChildId,
            c => c.Id,
            (_, c) => c);
}

this works perfectly fine with line v2.

would highly appreciate a workaround until this can be fixed.

thanks!



 Comments   
Comment by Githook User [ 07/Oct/22 ]

Author:

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

Message: CSHARP-4304: Handle conversions to an interface.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/f9fd1bef0a45354ca59e32e4f0ba81d6f196e27f

Comment by Dmitry Lukyanov (Inactive) [ 31/Aug/22 ]

Thanks djnitehawk83@gmail.com for your report, we will look at it and come back to you.

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