Wednesday, May 13, 2015

How to check if IQueryable result set is null or empty

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
}

2 comments:

  1. That answer does not work with IQueryables, just with list. For some of us new guys when the question is "how to check IQueryable" we need to know exactly how to get from point a to point b. If it must first be converted to a list, how is that accomplished? The above answer does not directly respond to the question at all.

    ReplyDelete
  2. Thanks, I will update the answer

    ReplyDelete