What is the best way to check IQueryable result set is null What is the best way to check IQueryable result set is null asp.net asp.net

What is the best way to check IQueryable result set is null


list will never be null with LINQ; it will simply represent an "empty collection" if need be. The way to test is with the Any extension method:

if (list.Any()) {    // list has at least one item}


An exception will be thrown if IQueryable yeilds no result. I use:

using System.Data.Entity; //for Async support in EFvar tQ = await _tableRepository.DisplayAll();try { return await tQ.ToListAsync(); }catch { return null; }

to trap the exception and return null; or an empty List if you prefer,

catch { return new List<Table>(); }


Here is what works for me:

    public IQueryable SomeFunc()    {        IQueryable result = Repo.SomeLinqQuery();        if (result.GetEnumerator().MoveNext() == false)        {            throw new Exception("Results empty");        }        return result;    }