Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .idea/.idea.OptimizeMePlease/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 46 additions & 1 deletion BenchmarkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public BenchmarkService()
/// and all his/her books (Book Name/Title and Publishment Year) published before 1900
/// </summary>
/// <returns></returns>
[Benchmark]
[Benchmark(Baseline = true)]
public List<AuthorDTO> GetAuthors()
{
using var dbContext = new AppDbContext();
Expand Down Expand Up @@ -97,6 +97,51 @@ public List<AuthorDTO> 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<AuthorDto_Optimized> 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<AuthorDTO_Optimized> GetAuthors_Optimized()
Expand Down
24 changes: 24 additions & 0 deletions DTOs.cs
Original file line number Diff line number Diff line change
@@ -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<BookStruct> Books { get; set; }
}

public struct BookStruct
{
public int AuthorId { get; set; }
public string Title { get; set; }
public int PublishedYear { get; set; }
}
}
3 changes: 2 additions & 1 deletion OptimizeMePlease.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>11</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
7 changes: 7 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sdk": {
"version": "7.0.0",
"rollForward": "latestMajor",
"allowPrerelease": true
}
}