From d59c408198fc1bb6e57177535d63e544d3a3f416 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Jovanovi=C4=87?= Date: Sat, 1 Oct 2022 20:33:34 +0200 Subject: [PATCH 1/2] 233x improvement --- BenchmarkService.cs | 29 ++++++++++++++++++++++++++++- OptimizeMePlease.csproj | 10 +++++----- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/BenchmarkService.cs b/BenchmarkService.cs index d0a958d..8ad3331 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -88,7 +88,34 @@ public List GetAuthors() [Benchmark] public List GetAuthors_Optimized() { - List authors = new List(); + using var dbContext = new AppDbContext(); + + var authors = dbContext + .Authors + .Include(x => x.Books.Where(b => b.Published.Year < 1900)) + .Where(x => + x.Country == "Serbia" && + x.Age == 27) + .OrderByDescending(x => x.BooksCount) + .Select(x => new AuthorDTO + { + UserFirstName = x.User.FirstName, + UserLastName = x.User.LastName, + UserEmail = x.User.Email, + UserName = x.User.UserName, + AllBooks = x.Books + .Select(y => new BookDto + { + Id = y.Id, + Name = y.Name, + Published = y.Published + }).ToList(), + AuthorAge = x.Age, + AuthorCountry = x.Country, + Id = x.Id + }) + .Take(2) + .ToList(); return authors; } diff --git a/OptimizeMePlease.csproj b/OptimizeMePlease.csproj index 7e4fcc9..c898ab0 100644 --- a/OptimizeMePlease.csproj +++ b/OptimizeMePlease.csproj @@ -2,16 +2,16 @@ Exe - netcoreapp3.1 + net6.0 - - - - + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive From b446dea700dae4abf169f4ee7a6ac4dac38668b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Jovanovi=C4=87?= Date: Tue, 4 Oct 2022 08:26:31 +0200 Subject: [PATCH 2/2] Filtering books in-memory --- BenchmarkService.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/BenchmarkService.cs b/BenchmarkService.cs index 8ad3331..8eb230d 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -92,7 +92,6 @@ public List GetAuthors_Optimized() var authors = dbContext .Authors - .Include(x => x.Books.Where(b => b.Published.Year < 1900)) .Where(x => x.Country == "Serbia" && x.Age == 27) @@ -117,6 +116,11 @@ public List GetAuthors_Optimized() .Take(2) .ToList(); + foreach (var author in authors) + { + author.AllBooks = author.AllBooks.Where(b => b.Published.Year < 1900).ToList(); + } + return authors; } }