From 3b43b428fa3bcef7aef4e3ddfda73cdcb1bd3f84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ali=20G=C3=B6l?= Date: Tue, 27 Sep 2022 20:02:41 +0300 Subject: [PATCH 1/2] Optimization added --- BenchmarkService.cs | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/BenchmarkService.cs b/BenchmarkService.cs index d0a958d..88e0aa3 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -85,11 +85,43 @@ public List GetAuthors() return finalAuthors; } - [Benchmark] - public List GetAuthors_Optimized() + [Benchmark] + public object GetAuthors_Optimized() { - List authors = new List(); - + using var dbContext = new AppDbContext(); + + var authors = dbContext + .Authors + .Where(x => x.Country == "Serbia" && x.Age == 27) + .Select(x => new AuthorDTO + { + UserCreated = x.User.Created, + UserEmailConfirmed = x.User.EmailConfirmed, + UserFirstName = x.User.FirstName, + UserLastActivity = x.User.LastActivity, + UserLastName = x.User.LastName, + UserEmail = x.User.Email, + UserName = x.User.UserName, + UserId = x.User.Id, + RoleId = x.User.UserRoles.FirstOrDefault().RoleId, + BooksCount = x.BooksCount, + AllBooks = x.Books.Where(c => c.Published.Year < 1900).Select(y => new BookDto + { + Id = y.Id, + Name = y.Name, + Published = y.Published, + ISBN = y.ISBN, + PublishedYear = y.Published.Year, + PublisherName = y.Publisher.Name + }).ToList(), + AuthorAge = x.Age, + AuthorCountry = x.Country, + AuthorNickName = x.NickName, + Id = x.Id + }) + .OrderByDescending(c => c.BooksCount) + .Take(2) + .ToArray(); return authors; } } From efb53d0283fa12126be51d49df4fc3c9e90c6a37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ali=20G=C3=B6l?= Date: Tue, 27 Sep 2022 20:04:07 +0300 Subject: [PATCH 2/2] Some fix --- BenchmarkService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BenchmarkService.cs b/BenchmarkService.cs index 88e0aa3..75c58ad 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -85,8 +85,8 @@ public List GetAuthors() return finalAuthors; } - [Benchmark] - public object GetAuthors_Optimized() + [Benchmark] + public AuthorDTO[] GetAuthors_Optimized() { using var dbContext = new AppDbContext();