-
Type:
Bug
-
Resolution: Done
-
Priority:
Major - P3
-
None
-
Affects Version/s: 2.1
-
Component/s: LINQ
-
None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
public static async Task TestAsync() { var client = new MongoClient(); var db = client.GetDatabase("test"); var col = db.GetCollection<User>("users"); await db.DropCollectionAsync("users"); await col.InsertOneAsync(new User { Id = "1", AppIntegrations = new[] { new UserAppIntegration { AppName = "App1", CanRemove = true }, new UserAppIntegration { AppName = "App2", CanRemove = false }, } }); var pipeline = col.Aggregate() .Match(u => u.Id == "1") .Project(u => u.AppIntegrations.Select(i => new UserAppIntegrationStatus { AppName = i.AppName, CanRemove = !i.CanRemove })); var results = await pipeline.ToListAsync(); //warm up Console.ReadKey(); } public class User { public User() { AppIntegrations = new UserAppIntegration[0]; } public string Id { get; set; } public string Email { get; set; } public UserAppIntegration[] AppIntegrations { get; set; } } public class UserAppIntegration { public string AppName { get; set; } public bool CanRemove { get; set; } } public class UserAppIntegrationStatus { public string AppName { get; set; } public bool CanRemove { get; set; } }
Workaround
Instead of projecting the enumerable directly, wrap it in a type:
var pipeline = col.Aggregate() .Match(u => u.Id == "1") .Project(u => new { SomeField = u.AppIntegrations.Select(i => new UserAppIntegrationStatus { AppName = i.AppName, CanRemove = !i.CanRemove }) });