Randy Minder
Randy Minder

Reputation: 48402

Which use of Linq Any() is more efficient?

I have a Linq query which looks as follows:

return this._alarmObjectAlarmViolationList
   .Where(row => row.ObjectId == subId)
   .Where(row => row.AlarmInternalId == "WECO #1 (StdDev > UCL)")
   .Where(row => row.PositionInSequence == row.SequenceCount)
   .Any();

Is this functionaly any different, or any more or less efficient, than putting the Where predicates inside the Any() statement?

This is a Linq to Objects query.

Thank you.

Upvotes: 2

Views: 339

Answers (1)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174309

No. There is no real difference in execution speed between .Where(predicate).Any() and Any(predicate).

However, using only Any(predicate) internally requires less objects to be created.

My suggestion is that you use the variant that is more readable. With many complicated predicates, I think it is easier to read when you use Where - as in your sample.

Please note: My answer only applies to LINQ to Objects.

Upvotes: 6

Related Questions