Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to determined null or count 0

Tags:

c#

linq

I have a iQueryable and I need to know if it's null or has no values.

IQueryable<people> L = (from p in people 
                        where p.lastname.Contains("y") 
                        select p);
if (L != null && L.Count() > 0) {
   return "Something";
} else {
   return "NOTHING";
}

Well if you use the L.Count() it will use more resources. Is there a better way? Something that does not use L.Count()

like image 796
Kramer Avatar asked Feb 23 '12 14:02

Kramer


1 Answers

It's recommended that you use .Any().

IQueryable<people> L = (from p in people 
                        where p.lastname.Contains("y") 
                        select p);
if (L.Any()) {
   return "Something";
} else {
   return "NOTHING";
}
like image 171
moribvndvs Avatar answered Nov 02 '22 03:11

moribvndvs