Open utterances-bot opened 1 year ago
The logic says FIIRST
I wouldn’t consider Sort
to be LINQ as it’s a native method on list rather than an extension method, but great article otherwise! I would be very surprised if First
wasn’t more efficient than Single
Right, John, Sort is not actually part of LINQ. But, often, devs tend to use what the Intellisense suggests, so I wanted to make clear the difference between Sort and OrderBy
First() only steps thru the collection as far as the first match item. Single() (normally) has to step thru the entire collection to ensure only one match is found. (It can exit early if it finds a second match, but it's going to throw an exception in that case).
Similarly, Any() steps through the collection only until the predicate condition is met, while Count() going thru the entire collection.
@john-h-k
"LINQ" stands for Language INtegrated Query" which basically means just the keywords from
, where
, select
, order by
etc. are really "LINQ", not the methods at all.
@jamescurran that’s a good point. I’ve personally always viewed LINQ to encompass both the language features and all the functionality under System.LINQ
. The only reason I mentioned sort was that it doesn’t fit what I would consider the “principles” of LINQ, which at a minimum are non-mutation, laziness, and chainability.
LINQ for beginners: pick the right methods! - Code4IT
LINQ is a set of methods that help developers perform operations on sets of items. There are tons of methods - do you know which is the one for you?
https://www.code4it.dev/blog/linq-differences