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