code4it-dev / blog-comments

https://www.code4it.dev/
1 stars 0 forks source link

blog/linq-differences #46

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

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

Rodrigo-Traverso commented 1 year ago

The logic says FIIRST

john-h-k commented 1 year ago

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

bellons91 commented 1 year ago

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

jamescurran commented 1 year ago

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.

jamescurran commented 1 year ago

@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.

john-h-k commented 1 year ago

@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.