Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

linq

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?

like image 234
niknowj Avatar asked Dec 13 '22 10:12

niknowj


1 Answers

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);
like image 139
George Duckett Avatar answered Mar 02 '23 01:03

George Duckett