[CSHARP-4691] Support GetType comparison in LINQ3 Created: 26/Jun/23  Updated: 28/Oct/23  Resolved: 29/Jun/23

Status: Closed
Project: C# Driver
Component/s: LINQ3
Affects Version/s: 2.20.0
Fix Version/s: 2.21.0

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

Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

We recently updated the NuGet package to the latest version (2.20.0) and noticed that one query doesn't work anymore. After some investigation I read that LINQ3 is now enabled by default. When I change the configuration to use LINQ2, the query works fine.

How to Reproduce

This is the query that works fine with LINQ2:

public async Task<IList<Activity>> GetActivitiesAsync<TActivityObject, TId>(TId objectId, CancellationToken cancellationToken = default)
    where TActivityObject : ActivityObject<TId>
    where TId : notnull, IEquatable<TId>, new()
{
    return await _client.ActivityCollection
            .Find(x => x.Object != null && x.Object.GetType() == typeof(TActivityObject) && ((TActivityObject)x.Object).Id.Equals(objectId))
            .SortByDescending(x => x.CreatedAt)
            .ToListAsync(cancellationToken);
}

With LINQ3 we get this exception:

MongoDB.Driver.Linq.ExpressionNotSupportedException : Expression not supported: x.Object.GetType()

I tried to use the "is" operator instead, but it also works only with LINQ2. With LINQ3 I get this exception:

System.Reflection.AmbiguousMatchException : Ambiguous match found.



 Comments   
Comment by Githook User [ 29/Jun/23 ]

Author:

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

Message: CSHARP-4691: Support GetType comparison in LINQ3.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/a24f1cdb44ed6093f666e0f3015f7d98b800a1ef

Comment by Bernd Hirschmann [ 27/Jun/23 ]

Hi, great to hear that you were able to add the support for GetType. I will test both cases in our project as soon as a new version is available. If you have a pre-release version, I can test it if you want. If I still have an issue, I can provide a small sample project.

Comment by Robert Stam [ 27/Jun/23 ]

I have support for GetType comparison filters working in LINQ3.

I did not encounter the issue you reported when using `is`. It mostly worked, though I did make a small fix to support `is` matching subclasses also.

Comment by Robert Stam [ 27/Jun/23 ]

In order to reproduce this I have assumed that `x.Object` is either of type `object` or of type `ActivityObject<int>`.

The scenarios are slightly different so I am testing both.

Comment by Robert Stam [ 26/Jun/23 ]

I'm trying to reproduce this but don't have quite enough information. For example, what is the compile time type of `x.Object`?

Can you provide a small standalone console application reproducing the issue?

Comment by PM Bot [ 26/Jun/23 ]

Hi bernd@guidnew.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:03 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.