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

Support GetType comparison in LINQ3

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 2.21.0
    • Affects Version/s: 2.20.0
    • Component/s: LINQ3
    • None
    • Not Needed
    • Hide

      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?

      Show
      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?

      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.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            bernd@guidnew.com Bernd Hirschmann
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: