[CSHARP-2878] Aggregate Project with object and Id Created: 17/Dec/19  Updated: 28/Oct/23  Resolved: 15/Feb/22

Status: Closed
Project: C# Driver
Component/s: Linq, Serialization
Affects Version/s: 2.10.0
Fix Version/s: 2.14.0

Type: Bug Priority: Major - P3
Reporter: Klaus Prünster Assignee: Unassigned
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

MongoDb on Cent Os 8 with C# 8 .net core 3.0


Epic Link: CSHARP-3615

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



 Comments   
Comment by Klaus Prünster [ 15/Feb/22 ]

Thats awesome. Thanks for the update!

Comment by James Kovacs [ 15/Feb/22 ]

This issue has been fixed in the new LINQ provider (known as LINQ3), which is included in the 2.14 release.

Configure your MongoClientSettings to use LinqProvider.V3 if you want to use this functionality.

To configure a client to use the LINQ3 provider use code like the following

var connectionString = "mongodb://localhost";
var clientSettings = MongoClientSettings.FromConnectionString(connectionString);
clientSettings.LinqProvider = LinqProvider.V3;
var client = new MongoClient(clientSettings);

Comment by Robert Stam [ 25/Mar/20 ]

I can reproduce this, but I don't have a fix for it at this time.

Comment by Klaus Prünster [ 17/Dec/19 ]

Sorry for the badly written issue, i committed it by accident and I cannot modify it...

Here is my model class structure:

 public class Article public class Article { public int Count { get; set; } public string Code { get; set; } public string SearchText { get; set; } public string NameDe { get; set; } public string NameIt { get; set; } public string NameEn { get; set; } public string Attribute1 { get; set; } public string Attribute2 { get; set; } }
 public class CustomerArticles { public string Id { get; set; } public IEnumerable<Article> Articles { get; set; } }

Generated at Wed Feb 07 21:43:46 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.