niknowj
niknowj

Reputation: 1037

How to check IEnumerable<DataRow> returns null or has any row?

i have a linq query similar like below.

IEnumerable<DataRow> query= (from item in IItemsTable.AsEnumerable()
                         where somecondition
                         select item);

how to check query contains any row or is empty?

Upvotes: 5

Views: 10085

Answers (1)

George Duckett
George Duckett

Reputation: 32428

You can use the extension method Any():

if(query.Any())
{
    //query has results.
}

Note that if you only care whether there are rows or not (and don't subsequently do something with those rows) you can use another overload of Any() to do it in one line:

bool queryhasresults = IItemsTable.AsEnumerable().Any(item => somecondition);

Upvotes: 17

Related Questions