2

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?

4
  • Performance question.... best would be to check with a Stopwatch or like wise and post it as an answer,
    – V4Vendetta
    Commented Aug 27, 2012 at 9:09
  • There is actually no need to check for 'Any()'. You should however check the the 'IQueryable<Deal>' is not null!
    – Franky
    Commented Aug 27, 2012 at 9:19
  • The first three rules of performance optimization: measure, measure, measure.
    – SWeko
    Commented Aug 27, 2012 at 9:23
  • @BigYellowCactus - I could test it but it would probably give different results in different scenarios - I was looking for a "is it better practice" answer
    – dotnetnoob
    Commented Aug 27, 2012 at 15:07

2 Answers 2

6

I cannot see a case where it would be faster. If Any returns false, the list would have contained no element anyway. So you don't save network bandwidth, and it is unlikely that the server-side execution plan is that much better.

If Any returns true you just have introduced an unnecessary round-trip and and unnecessary query execution.

It could be faster by coincidence but not in a predictable, worthwhile way.

1
  • Thanks usr - similar answer to above which was first, but voted up.
    – dotnetnoob
    Commented Aug 27, 2012 at 15:11
2

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.

1
  • Thats what I was looking for, a real world answer.
    – dotnetnoob
    Commented Aug 27, 2012 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.