c# - When to check List<T> for null, when for 0 and when both -


i use ado.net entity framework , there code snippets :

list<sole> entity = soleservice.all()     .where(s => (s.shoelastid == shoelastid) && (s.status == 20))     .tolist(); 

since haven't think , made check:

if (entity.count > 0) 

believing enough. see many people check any() , null. how sure @ situation kind of checks need , in scenario said - use if (entity.count > 0) enough?

if (entity.count > 0) or if (entity.any()) identical in case. fetched data db, list has been built , knows size. .count property doesn't iterate on anything.

in other hand, not call .count() ienumerable extension if didn't fetched data, because it'll enumerate items nothing.

use instead:

bool test = soleservice.all()     .any(s => (s.shoelastid == shoelastid) && (s.status == 20));  if (test) { ... } 

also, linq extensions won't return null empty ienumerable, don't check null.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -