Skip to content

Instantly share code, notes, and snippets.

@nishanc
Created December 19, 2021 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nishanc/8ca23339b0e505662d2ca21d93111a58 to your computer and use it in GitHub Desktop.
Save nishanc/8ca23339b0e505662d2ca21d93111a58 to your computer and use it in GitHub Desktop.
[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