c# - Does Any() stop on success? -


to more specific: linq extension method any(ienumerable collection, func predicate) stop checking remaining elements of collections once predicate has yielded true item?

because don't want spend time on figuring out if need expensive parts @ all:

if(lotsofitems.any(x => x.id == target.id))     //do expensive calculation here 

so if any checking items in source might end being waste of time instead of going with:

var candidate = lotsofitems.firstordefault(x => x.id == target.id) if(candicate != null)      //do expensive calculation here 

because i'm pretty sure firstordefault return once got result , keeps going through whole enumerable if not find suitable entry in collection.

does anyonehave information internal workings of any, or suggest solution kind of decision?

also, colleague suggested along lines of:

if(!lotsofitems.all(x => x.id != target.id)) 

since supposed stop once conditions returns false first time i'm not sure on that, if shed light on appreciated.

as see source code, yes:

 internal static bool any<t>(this ienumerable<t> source, func<t, bool> predicate) {             foreach (t element in source) {                 if (predicate(element)) {                     return true; // attention line                 }             }             return false;         } 

any() efficient way determine whether element of sequence satisfies condition linq.

also:a colleague suggested along lines of

if(!lotsofitems.all(x => x.id != target.id)) since supposed stop once conditions returns false first time i'm not sure on that, if shed light on appreciated :>]

all() determines whether elements of sequence satisfy condition. so, enumeration of source stopped result can determined.

additional note:
above true if using linq objects. if using linq database, create query , execute against database.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -