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

Linq join error when add .Where() to foreign collection queryable

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Critical - P2 Critical - P2
    • None
    • Affects Version/s: 3.0.0
    • Component/s: Linq, LINQ3
    • None
    • Dotnet Drivers

      Driver version: 3.0.0 (I've tried all versions between 2.20.0 to 3.0.0 and none of them worked)

      .NET SDK Version 8.0.403

      Trivial example:

      BsonClassMap.RegisterClassMap<Pages>(cm =>
      {
          cm.AutoMap();
          cm.MapIdMember(c => c.Id);
      });
      
      BsonClassMap.RegisterClassMap<PageContainer>(cm =>
      {
          cm.AutoMap();
          cm.MapIdMember(c => c.Id);
      });
      
      var client = new MongoClient("mongodb://localhost:27017");
      var database = client.GetDatabase("playground");
      
      var pages = database.GetCollection<Pages>("pages");
      var pageContainers = database.GetCollection<PageContainer>("pagecontainers");
      
      var pagesWithContainers = await (from page in pages.AsQueryable().Where(x => x.TenantId == 1)
          join container in pageContainers.AsQueryable() on page.Id equals container.PageId into containers
          select new
          {
              page.Id,
              page.Title,
              Containers = containers
          }).ToListAsync();  
      
      Console.ReadLine();
      
      public class Pages
      {
          public required ObjectId Id { get; init; }
          public required string Title { get; init; }
      }
      
      public class PageContainer
      {
          public required ObjectId Id { get; init; }
          public required ObjectId PageId { get; init; }
          public required string Content { get; init; }
      } 

      The exception i get:

       

      Unhandled exception. MongoDB.Driver.Linq.ExpressionNotSupportedException: Expression not supported: playground.pagecontainers.Aggregate([]).Where(x => (x.TenantId == 1)) in playground.pages.Aggregate([]).Where(x => (x.TenantId == 1)).GroupJoin(playground.pagecontainers.Aggregate([]).Where(x => (x.TenantId == 1)), page => page.Id, container => container.PageId, (page, containers) => new <>f__AnonymousType0`3(Id = page.Id, Title = page.Title, Containers = containers)) because inner expression must be a MongoDB IQueryable against a collection.
         at MongoDB.Driver.Linq.Linq3Implementation.ExtensionMethods.ExpressionExtensions.GetCollectionInfo(Expression innerExpression, Expression containerExpression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.GroupJoinMethodToPipelineTranslator.Translate(TranslationContext context, MethodCallExpression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.ExpressionToPipelineTranslator.Translate(TranslationContext context, Expression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.ExpressionToExecutableQueryTranslator.Translate[TDocument,TOutput](MongoQueryProvider`1 provider, Expression expression, ExpressionTranslationOptions translationOptions)
         at MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2.ToCursorAsync(CancellationToken cancellationToken)
         at MongoDB.Driver.IAsyncCursorSourceExtensions.ToListAsync[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)
         at Program.<Main>$(String[] args) in /Users/keremacer/Projects/Playground/Playground/Program.cs:line 25
         at Program.<Main>(String[] args)
        

       

      If I remove ".Where(x => x.TenantId == 1)" from pageContainers.AsQueryable(), it works

            Assignee:
            Unassigned Unassigned
            Reporter:
            keremacer1@gmail.com kerem N/A
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: