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

Aggregate Project with object and Id

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 2.14.0
    • Affects Version/s: 2.10.0
    • Component/s: Linq, Serialization
    • None
    • Environment:
      MongoDb on Cent Os 8 with C# 8 .net core 3.0
    • None
    • None
    • None
    • None
    • None
    • None

      Aggregation with Project and anonymous type works

      var j = _context.GetCollection<CustomerArticles>("Worked_CustomersArticles") .Aggregate() .Project(c => new    {  Id = c.Id,  Articles = c.Articles.Where(c => c.Code == "12")   }) .Limit(5) .ToList();

       

      Aggregation with Project and strongly typed class  does not work

      var j = _context.GetCollection<CustomerArticles>("Worked_CustomersArticles")var j = _context.GetCollection<CustomerArticles>("Worked_CustomersArticles") .Aggregate() .Project(c => new CustomerArticles   { Id = c.Id, Articles = c.Articles.Where(c => c.Code == "12")   }) .Limit(5) .ToList();
      

       

      Exception:

      System.FormatException: 'Element 'Id' does not match any field or property of class QueryTester.CustomerArticles.'

       

      But I found a workaround:

       var j = _context.GetCollection<CustomerArticles>("Worked_CustomersArticles") var j = _context.GetCollection<CustomerArticles>("Worked_CustomersArticles") .Aggregate() .Project(c => new   {   Id = c.Id,   Articles = c.Articles.Where(c => c.Code == "12")   }) .Project(c => new CustomerArticles   {   Id = c.Id,   Articles = c.Articles.Where(c => c.Code == "12")   }) .Limit(5) .ToList();
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            klaus.pruenster@gmail.com Klaus Prünster
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: