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
27 changes: 26 additions & 1 deletion BenchmarkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,32 @@ public List<AuthorDTO> GetAuthors()
[Benchmark]
public List<AuthorDTO> GetAuthors_Optimized()
{
List<AuthorDTO> authors = new List<AuthorDTO>();
using var dbContext = new AppDbContext();

var authors = dbContext.Authors
.Include(x => x.User)
.Include(x => x.Books)
.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,
BooksCount = x.BooksCount,
AllBooks = x.Books
.Where(a => a.Published.Year < 1900)
.Select(y => new BookDto
{
Name = y.Name,
PublishedYear = y.Published.Year
}).ToList(),
AuthorAge = x.Age,
AuthorCountry = x.Country
})
.Take(2)
.ToList();

return authors;
}
Expand Down
2 changes: 1 addition & 1 deletion Context/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class AppDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer("Server=localhost;Database=OptimizeMePlease;Trusted_Connection=True;Integrated Security=true;MultipleActiveResultSets=true");
options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=OptimizeMePlease;Trusted_Connection=True;MultipleActiveResultSets=true");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand Down
5 changes: 3 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ static void Main(string[] args)
//Debugging
BenchmarkService benchmarkService = new BenchmarkService();
benchmarkService.GetAuthors();
benchmarkService.GetAuthors_Optimized();

//Comment me after first execution, please.
//IWillPopulateData();

//BenchmarkRunner.Run<BenchmarkService>();
BenchmarkRunner.Run<BenchmarkService>();
}

public static void IWillPopulateData()
{
string sqlConnectionString = @"Server=localhost;Database=OptimizeMePlease;Trusted_Connection=True;Integrated Security=true;MultipleActiveResultSets=true";
string sqlConnectionString = @"Server=(localdb)\MSSQLLocalDB;Database=OptimizeMePlease;Trusted_Connection=True;MultipleActiveResultSets=true";

string workingDirectory = Environment.CurrentDirectory;
string path = Path.Combine(Directory.GetParent(workingDirectory).Parent.Parent.FullName, @"script.sql");
Expand Down