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

Distinct followed by Count is not supported in a LINQ query but the error message is wrong

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 1.6
    • Affects Version/s: 1.5
    • Component/s: None
    • None

      See the discussion in mongodb-csharp:

      https://groups.google.com/forum/?fromgroups#!topic/mongodb-csharp/oeYHjPB7O6Q

      To reproduce:

      public class C
      {
          public int Id;
          public int X;
      }
      
      collection.Insert(new C { Id = 1, X = 1 });
      collection.Insert(new C { Id = 2, X = 2 });
      
      var count = collection.AsQueryable()
          .Select(a => a.X)
          .Distinct()
          .Count();
      

      The exception thrown is:

      System.NotSupportedException: Distinct must be used with Select to identify the field whose distinct values are to be found.
         at MongoDB.Driver.Linq.SelectQuery.ExecuteDistinct(IMongoQuery query) in C:\work\rstam\mongo-csharp-driver\Driver\Linq\Translators\SelectQuery.cs:line 302
         at MongoDB.Driver.Linq.SelectQuery.Execute() in C:\work\rstam\mongo-csharp-driver\Driver\Linq\Translators\SelectQuery.cs:line 137
         at MongoDB.Driver.Linq.MongoQueryProvider.Execute(Expression expression) in C:\work\rstam\mongo-csharp-driver\Driver\Linq\MongoQueryProvider.cs:line 155
         at MongoDB.Driver.Linq.MongoQueryProvider.Execute[TResult](Expression expression) in C:\work\rstam\mongo-csharp-driver\Driver\Linq\MongoQueryProvider.cs:line 131
         at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
         at ConsoleApplication1.Program.Main(String[] _args) in C:\Users\Robert Stam\documents\visual studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Pro
      gram.cs:line 39
      

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            robert@mongodb.com Robert Stam
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: