Created
December 19, 2021 18:09
-
-
Save nishanc/8ca23339b0e505662d2ca21d93111a58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[MemoryDiagnoser] | |
public class UnnecessaryJoinsBenchmark : Benchmark | |
{ | |
[Benchmark] | |
public async Task<List<CustomView>> WithJoins() | |
{ | |
var books = await (from authors in Context.Authors | |
where authors.Id > 5 | |
join book in Context.Books on authors.Id equals book.AuthorId select book) | |
.Select(x => new CustomView | |
{ | |
Title = x.Title, | |
AuthorFirstName = x.Author.FirstName | |
}).ToListAsync(); | |
return books; | |
} | |
[Benchmark] | |
public async Task<List<CustomView>> WithoutJoins() | |
{ | |
var authors = await (from author in Context.Authors where author.Id > 5 select author).Select(x => x).ToListAsync(); | |
var books = await (from book in Context.Books where authors.Contains(book.Author) select book) | |
.Select(x => new CustomView | |
{ | |
Title = x.Title, | |
AuthorFirstName = x.Author.FirstName | |
}).ToListAsync(); | |
return books; | |
} | |
public class CustomView | |
{ | |
public string Title { get; set; } | |
public string AuthorFirstName { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment