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

Aggregate Project with object and Id

    XMLWordPrintable

Details

    • Bug
    • Status: Closed
    • Major - P3
    • Resolution: Fixed
    • 2.10.0
    • 2.14.0
    • Linq, Serialization
    • None
    • MongoDb on Cent Os 8 with C# 8 .net core 3.0

    Description

      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();
      

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: