C
C#2y ago
linqisnice

❔ which of these queries is "better"? And will the second one work?

I'm trying to get the total results of the query before pagination to display it to the client. But when googling, some posts on stackoverflow said its impoossible without two separate queries. But it seems the second option is just one query? Or am i missing something in my code?

var filteredListings = await _dbContext.Listings
.Where(listing => listing.GetAvailableAccomodations(request.Guests).Any())
.WithinRadius(request.Longitude, request.Latitude, FilterUtilities.LocationRadius)
.Where(listing => listing.ListingQualities.Any(quality => request.FilterIds.Contains(quality.QualityId)))
.Paginate(request.Page, FilterUtilities.PageSize)
.Include(x => x.ListingQualities)
.ThenInclude(x => x.Quality)
.ToListAsync();

var totalSearchResults = await _dbContext.Listings
.Where(listing => listing.GetAvailableAccomodations(request.Guests).Any())
.WithinRadius(request.Longitude, request.Latitude, FilterUtilities.LocationRadius)
.Where(listing => listing.ListingQualities.Any(quality => request.FilterIds.Contains(quality.QualityId))).CountAsync();

var filteredListings = await _dbContext.Listings
.Where(listing => listing.GetAvailableAccomodations(request.Guests).Any())
.WithinRadius(request.Longitude, request.Latitude, FilterUtilities.LocationRadius)
.Where(listing => listing.ListingQualities.Any(quality => request.FilterIds.Contains(quality.QualityId)))
.Paginate(request.Page, FilterUtilities.PageSize)
.Include(x => x.ListingQualities)
.ThenInclude(x => x.Quality)
.ToListAsync();

var totalSearchResults = await _dbContext.Listings
.Where(listing => listing.GetAvailableAccomodations(request.Guests).Any())
.WithinRadius(request.Longitude, request.Latitude, FilterUtilities.LocationRadius)
.Where(listing => listing.ListingQualities.Any(quality => request.FilterIds.Contains(quality.QualityId))).CountAsync();
And this
var filteredListingsQuery = _dbContext.Listings
.GetListingsWithAvailableAccomodations(request.Guests, dateRange)
.WithinRadius(request.Longitude, request.Latitude, FilterUtilities.LocationRadius)
.Where(listing => listing.ListingQualities.Any(quality => request.FilterIds.Contains(quality.QualityId)));

var totalSearchResults = await filteredListingsQuery.CountAsync();

var filteredListings = await filteredListingsQuery.Paginate(request.Page, FilterUtilities.PageSize)
.Include(x => x.ListingQualities)
.ThenInclude(x => x.Quality)
.ToListAsync();
var filteredListingsQuery = _dbContext.Listings
.GetListingsWithAvailableAccomodations(request.Guests, dateRange)
.WithinRadius(request.Longitude, request.Latitude, FilterUtilities.LocationRadius)
.Where(listing => listing.ListingQualities.Any(quality => request.FilterIds.Contains(quality.QualityId)));

var totalSearchResults = await filteredListingsQuery.CountAsync();

var filteredListings = await filteredListingsQuery.Paginate(request.Page, FilterUtilities.PageSize)
.Include(x => x.ListingQualities)
.ThenInclude(x => x.Quality)
.ToListAsync();
4 Replies
Angius
Angius2y ago
The second option is also two queries Both .CountAsync() and .ToListAsync() resolve the query As for which is better... might sound picky but neither, since both use .Include() instead of a .Select() With a select you might actually even be able to get the count and the results in the same query
linqisnice
linqisniceOP2y ago
I'm not sure I get it. Im supposed to eager load some of these related entities for the final query. I need them both the listing and its qualities
Angius
Angius2y ago
Yeah, you can do that with .Select() That also loads eagerly Unless you're loading entities to update their related entities, don't use .Include(), always .Select() into a DTO
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server