From 353d7fa0e2733def94e73c3e73dd57b13c4a50a1 Mon Sep 17 00:00:00 2001 From: Andreas Coroiu Date: Fri, 3 Mar 2023 14:20:02 +0100 Subject: [PATCH 1/3] [PM-1191] fix: use join instead of select-subquery EF6 is currently not able to translate LINQ that include selects after groupby statements. Using join let's us bypass this issue. --- .../Repositories/CollectionRepository.cs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs b/src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs index 755e69264d9e..ce5b07b1c0a8 100644 --- a/src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs +++ b/src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs @@ -186,13 +186,13 @@ where cg.CollectionId.Equals(id) { var dbContext = GetDatabaseContext(scope); var groups = - from cg in dbContext.CollectionGroups - where cg.Collection.OrganizationId == organizationId + from c in collections + join cg in dbContext.CollectionGroups on c.Id equals cg.CollectionId group cg by cg.CollectionId into g select g; var users = - from cu in dbContext.CollectionUsers - where cu.Collection.OrganizationId == organizationId + from c in collections + join cu in dbContext.CollectionUsers on c.Id equals cu.CollectionId group cu by cu.CollectionId into u select u; @@ -230,19 +230,16 @@ group cu by cu.CollectionId into u { var dbContext = GetDatabaseContext(scope); var groups = - from cg in dbContext.CollectionGroups - where cg.Collection.OrganizationId == organizationId - && collections.Select(c => c.Id).Contains(cg.Collection.Id) + from c in collections + join cg in dbContext.CollectionGroups on c.Id equals cg.CollectionId group cg by cg.CollectionId into g select g; var users = - from cu in dbContext.CollectionUsers - where cu.Collection.OrganizationId == organizationId - && collections.Select(c => c.Id).Contains(cu.Collection.Id) + from c in collections + join cu in dbContext.CollectionUsers on c.Id equals cu.CollectionId group cu by cu.CollectionId into u select u; - return collections.Select(collection => new Tuple( collection, From f57443d8c40d349056948915504bff724e2def50 Mon Sep 17 00:00:00 2001 From: Andreas Coroiu Date: Fri, 3 Mar 2023 14:23:31 +0100 Subject: [PATCH 2/3] [PM-1191] chore: simplify queries --- .../Repositories/CollectionRepository.cs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs b/src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs index ce5b07b1c0a8..1d18d5311f69 100644 --- a/src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs +++ b/src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs @@ -188,13 +188,11 @@ where cg.CollectionId.Equals(id) var groups = from c in collections join cg in dbContext.CollectionGroups on c.Id equals cg.CollectionId - group cg by cg.CollectionId into g - select g; + select cg; var users = from c in collections join cu in dbContext.CollectionUsers on c.Id equals cu.CollectionId - group cu by cu.CollectionId into u - select u; + select cu; return collections.Select(collection => new Tuple( @@ -202,7 +200,6 @@ group cu by cu.CollectionId into u new CollectionAccessDetails { Groups = groups - .FirstOrDefault(g => g.Key == collection.Id)? .Select(g => new CollectionAccessSelection { Id = g.GroupId, @@ -210,7 +207,6 @@ group cu by cu.CollectionId into u ReadOnly = g.ReadOnly }).ToList() ?? new List(), Users = users - .FirstOrDefault(u => u.Key == collection.Id)? .Select(c => new CollectionAccessSelection { Id = c.OrganizationUserId, @@ -232,13 +228,11 @@ group cu by cu.CollectionId into u var groups = from c in collections join cg in dbContext.CollectionGroups on c.Id equals cg.CollectionId - group cg by cg.CollectionId into g - select g; + select cg; var users = from c in collections join cu in dbContext.CollectionUsers on c.Id equals cu.CollectionId - group cu by cu.CollectionId into u - select u; + select cu; return collections.Select(collection => new Tuple( @@ -246,7 +240,6 @@ group cu by cu.CollectionId into u new CollectionAccessDetails { Groups = groups - .FirstOrDefault(g => g.Key == collection.Id)? .Select(g => new CollectionAccessSelection { Id = g.GroupId, @@ -254,7 +247,6 @@ group cu by cu.CollectionId into u ReadOnly = g.ReadOnly }).ToList() ?? new List(), Users = users - .FirstOrDefault(u => u.Key == collection.Id)? .Select(c => new CollectionAccessSelection { Id = c.OrganizationUserId, From 5119608ed9a2ff32af8b3b3d0f9e0befc58164c6 Mon Sep 17 00:00:00 2001 From: Andreas Coroiu Date: Fri, 3 Mar 2023 15:26:58 +0100 Subject: [PATCH 3/3] Revert "[PM-1191] chore: simplify queries" This reverts commit f57443d8c40d349056948915504bff724e2def50. --- .../Repositories/CollectionRepository.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs b/src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs index 1d18d5311f69..ce5b07b1c0a8 100644 --- a/src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs +++ b/src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs @@ -188,11 +188,13 @@ where cg.CollectionId.Equals(id) var groups = from c in collections join cg in dbContext.CollectionGroups on c.Id equals cg.CollectionId - select cg; + group cg by cg.CollectionId into g + select g; var users = from c in collections join cu in dbContext.CollectionUsers on c.Id equals cu.CollectionId - select cu; + group cu by cu.CollectionId into u + select u; return collections.Select(collection => new Tuple( @@ -200,6 +202,7 @@ join cu in dbContext.CollectionUsers on c.Id equals cu.CollectionId new CollectionAccessDetails { Groups = groups + .FirstOrDefault(g => g.Key == collection.Id)? .Select(g => new CollectionAccessSelection { Id = g.GroupId, @@ -207,6 +210,7 @@ join cu in dbContext.CollectionUsers on c.Id equals cu.CollectionId ReadOnly = g.ReadOnly }).ToList() ?? new List(), Users = users + .FirstOrDefault(u => u.Key == collection.Id)? .Select(c => new CollectionAccessSelection { Id = c.OrganizationUserId, @@ -228,11 +232,13 @@ join cu in dbContext.CollectionUsers on c.Id equals cu.CollectionId var groups = from c in collections join cg in dbContext.CollectionGroups on c.Id equals cg.CollectionId - select cg; + group cg by cg.CollectionId into g + select g; var users = from c in collections join cu in dbContext.CollectionUsers on c.Id equals cu.CollectionId - select cu; + group cu by cu.CollectionId into u + select u; return collections.Select(collection => new Tuple( @@ -240,6 +246,7 @@ join cu in dbContext.CollectionUsers on c.Id equals cu.CollectionId new CollectionAccessDetails { Groups = groups + .FirstOrDefault(g => g.Key == collection.Id)? .Select(g => new CollectionAccessSelection { Id = g.GroupId, @@ -247,6 +254,7 @@ join cu in dbContext.CollectionUsers on c.Id equals cu.CollectionId ReadOnly = g.ReadOnly }).ToList() ?? new List(), Users = users + .FirstOrDefault(u => u.Key == collection.Id)? .Select(c => new CollectionAccessSelection { Id = c.OrganizationUserId,