I have methods that then chain on to others, passing IQueryable. Here’s a cut down sample.
public static IQueryable<Deal> Deals(this DbContext context)
{
Guard.ThrowIfNull(context, "context");
var r = new ReadRepo<Deal>(context);
return r.FindBy()
.Include("Deals_SitePost")
.Include("Deals_CommunityPost")
.Include("Deals_Preorder")
.Include("Deals_Product")
.Include("Deals_Sale")
.Include("Deals_VoucherCode")
.Include("DealSubcategories");
}
public static IQueryable<Deal> ByStore(this IQueryable<Deal> deals, int storeId)
{
return deals.Where(d => d.StoreId == storeId);
}
public static IQueryable<Deal> WhereFeatured(this IQueryable<Deal> deals)
{
return deals.Where(d => d.Deals_SitePost.IsNotNull() && d.Deals_SitePost.IsFeatured);
}
As you can see, there is a starter ‘Deals’ and a couple of extension methods that I use to extend the query.
In this scenario is is wise to check and return where !IQueryable.Any() in terms of performance before the query statement, or will it not matter?
It might be faster, if
Any() == true, but the difference will probably be negligible.On the other hand it can be significantly slower, if the query returns something because you then have two roundtrips to the database. One to query if there is anything at all, second to actually retrieve the data.