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

Using Contains with GroupBy and an Aggregate throws NotSupportedException

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.3
    • Affects Version/s: 2.2.3
    • Component/s: Linq
    • None

      The following query works:

      return Database
          .GetCollection<MyEntity>()
          .AsQueryable()
          .Where(x => x.StartDate <= instance && x.EndDate >= instance)
          .GroupBy(x => x.Key.Guid)
          .Select(x => x.First().Id)
          .ToList();
      

      But, when adding an $in condition (see below), the following exception is thrown:

      An unhandled exception was thrown by the application. System.NotSupportedException: $project or $group does not support First({document}{_id})
      
      return Database
          .GetCollection<MyEntity>()
          .AsQueryable()
          .Where(x => guidKeys.Contains(x.Key.Guid)) // $in condition
          .Where(x => x.StartDate <= instance && x.EndDate >= instance)
          .GroupBy(x => x.Key.Guid)
          .Select(x => x.First().Id)
          .ToList();
      

      I understand that a lot of LINQ is not yet supported by the driver, but I can't understand how introducing that addition match stage (using $in) could result in an incompatibility for the grouping stage.

      Is anyone able to explain why this is happening?

      I am using MongoDB 3.2 with the .NET Driver 2.2.2.

      MyEntity looks something like this:

      [BsonIgnoreExtraElements]
      public class MyEntity: BaseMongoDocument
      {
          [BsonId]
          [BsonRepresentation(BsonType.Binary)]
          public Guid Id { get; set; }
      
          [BsonRequired]
          [BsonElement("startDate")]
          public DateTime StartDate { get; set; }
      
          [BsonRequired]
          [BsonElement("endDate")]
          public DateTime EndDate { get; set; }
      
          [BsonRequired]
          [BsonElement("key")]
          public SquidDocument Key { get; set; }
      
          [BsonConstructor]
          private MyEntity()
          { }
      }
      
      public class SquidDocument
      {
          [BsonRequired]
          [BsonElement("guid")]
          public Guid Guid { get; private set; }
      
          [BsonRequired]
          [BsonElement("squid")]
          public string Squid { get; private set; }
      
          [BsonConstructor]
          private SquidDocument(Guid guid, string squid)
          {
              Guid = realSquid.Guid;
              Squid = realSquid.Value;
          }
      }
      

      Originally logged on StackOverflow: http://stackoverflow.com/questions/36838213/net-driver-with-linq-notsupportedexception-project-or-group

            Assignee:
            craig.wilson@mongodb.com Craig Wilson
            Reporter:
            davenewza Dave New
            Votes:
            2 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: