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

Quering for first item of GroupBy aggregation produces NotSupportedException

      I have entities with compound ids in my collection: {_id: {_id: guid, version: int}}. When I need to query for latest versions of objects with some guid identificators i use following LINQ query:

      Collection.AsQueryable()
          .Where(x => ids.Contains(x.Id.Id))
          .OrderByDescending(x => x.Id.Version)
          .GroupBy(x => x.Id.Id)
          .Select(x => x.First())
          .Select(x => new
          {
              Id = x.Id.Id,
              Title = x.Title
          }).ToList();
      

      With 2.3 driver version it works when I make projection in the end. But when I need to extract whole object it fails with NotSupportedException:

      Collection.AsQueryable()
          .Where(x => newIds.Contains(x.Id.Id))
          .OrderByDescending(x => x.Id.Version)
          .GroupBy(x => x.Id.Id)
          .Select(x => x.First)
          .ToList();
      

      I tried to workaround this with "fake" projection without success either:

      Collection.AsQueryable()
          .Where(x => newIds.Contains(x.Id.Id))
          .OrderByDescending(x => x.Id.Version)
          .GroupBy(x => x.Id.Id)
          .Select(x => new
          {
              Entity = x.First()
          })
          .AsEnumerable()
          .Select(x => x.Entity)
          .ToList();
      

      Any suggestions?

            Assignee:
            james.kovacs@mongodb.com James Kovacs
            Reporter:
            osovitskiy Oleg Osovitskiy
            Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: