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

Custom/unsupported collections with their own impl of .Contains(...) causes a LINQ error

    • Type: Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      When a .NET collection type, such as ISet, defines its own Contains method, the C# compiler will (rightly) choose this implementation over the Enumerable.Contains extension method. When the collection type is unsupported by MongoDB natively, however, Mongo's client driver for C# will throw an exception because it doesn't know how to translate the Contains method invocation into a query.

      The workaround at the moment is not too bad – calling Enumerable.Contains(collection, item to check) rather than collection.Contains(item to check) translates properly by the driver. However, this is a bit hackish and is definitely not easy to read.

      I think this is a situation where "if it looks like a duck and quacks like a duck, its a duck" applies. If a type implements IEnumerable<T>, defines a method called Contains, and that method takes one argument of type T, you should translate it just like you would do with the extension method or one of the supported collection types' Contains method.

            Assignee:
            Unassigned Unassigned
            Reporter:
            david@funnelfire.com David Pfeffer
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: