diff --git a/.idea/.idea.OptimizeMePlease/.idea/.gitignore b/.idea/.idea.OptimizeMePlease/.idea/.gitignore new file mode 100644 index 0000000..243280d --- /dev/null +++ b/.idea/.idea.OptimizeMePlease/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/contentModel.xml +/.idea.OptimizeMePlease.iml +/modules.xml +/projectSettingsUpdater.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/BenchmarkService.cs b/BenchmarkService.cs index a32a5c8..9bdb4c6 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -32,7 +32,7 @@ public BenchmarkService() /// and all his/her books (Book Name/Title and Publishment Year) published before 1900 /// /// - [Benchmark] + [Benchmark(Baseline = true)] public List GetAuthors() { using var dbContext = new AppDbContext(); @@ -97,6 +97,51 @@ public List GetAuthors() return finalAuthors; } + + /// Get top 2 Authors (FirstName, LastName, UserName, Email, Age, Country) + /// from country Serbia aged 27, with the highest BooksCount + /// and all his/her books (Book Name/Title and Publishment Year) published before 1900 + [Benchmark] + public List GetAuthors_Optimized_() + { + using var dbContext = new AppDbContext(); + + var orderedAuthors = dbContext.Authors.AsNoTracking() + .Where(x => x.Country == "Serbia" && x.Age == 27) + .Include(x => x.User) + .OrderByDescending(x=>x.BooksCount) + .Select(x => new AuthorDto_Optimized + { + + Id = x.Id, + FirstName = x.User.FirstName, + LastName = x.User.LastName, + Email = x.User.Email, + UserName = x.User.UserName, + UserId = x.UserId, + Age = x.Age, + Country = x.Country + }) + .Take(2).ToList(); + + + var userIdArray = orderedAuthors.Select(x => x.Id).ToArray(); + var dt = new DateTime(1900, 1, 1); + var books = dbContext.Books.Where(x => userIdArray.Contains(x.AuthorId) && x.Published < dt).Select(x => new BookStruct() + { + AuthorId = x.AuthorId, + Title = x.Name, + PublishedYear = x.Published.Year + }).ToList(); + + foreach (var auth in orderedAuthors) + { + auth.Books = books.Where(b => b.AuthorId == auth.Id).ToList(); + } + + return orderedAuthors; + } + //[Benchmark] //public List GetAuthors_Optimized() diff --git a/DTOs.cs b/DTOs.cs new file mode 100644 index 0000000..7aca585 --- /dev/null +++ b/DTOs.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; + +namespace OptimizeMePlease +{ + public class AuthorDto_Optimized + { + public int Id { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public string UserName { get; set; } + public string Email { get; set; } + public int UserId { get; set; } + public int Age { get; set; } + public string Country { get; set; } + public ICollection Books { get; set; } + } + + public struct BookStruct + { + public int AuthorId { get; set; } + public string Title { get; set; } + public int PublishedYear { get; set; } + } +} \ No newline at end of file diff --git a/OptimizeMePlease.csproj b/OptimizeMePlease.csproj index f603ecd..00b1727 100644 --- a/OptimizeMePlease.csproj +++ b/OptimizeMePlease.csproj @@ -2,7 +2,8 @@ Exe - netcoreapp3.1 + net7.0 + 11 diff --git a/global.json b/global.json new file mode 100644 index 0000000..7cd6a1f --- /dev/null +++ b/global.json @@ -0,0 +1,7 @@ +{ + "sdk": { + "version": "7.0.0", + "rollForward": "latestMajor", + "allowPrerelease": true + } +} \ No newline at end of file