C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C# LINQ
Review extension methods from the System.Linq namespace. Learn to write queries directly in programs.
LINQ. The bird calls out in search of a mate. It queries the surrounding area with its bird song. With LINQ we formulate queries in our programs.
Methods. In a query, we ask for something. We ask for an array to be filtered or sorted or changed. LINQ is Language Integrated Query—here are methods that encode these requests.AggregateAllAnyAsEnumerableAsParallelAsQueryableAverageCastConcatContainsCountDefaultIfEmptyDistinctElementAt, ElementAtOrDefaultExceptFirstFirstOrDefaultGroupByGroupJoinIntersectJoinLast, LastOrDefaultMax, MinOfTypeOrderBy, OrderByDescendingReverseSelectSelectManySequenceEqualSingleSingleOrDefaultSkip, SkipWhileSumTake, TakeWhileToArrayToDictionaryToListToLookupUnionWhereZip
Query keywords. Query expressions use a whole new set of keywords. These are contextual keywords. This means they only have meaning in query expressions.ascending, descendinggroupjoinletorderbyselect new
Enumerable. The Enumerable type has some useful static methods. If you need an IEnumerable collection of a range or a repeated element, consider Range or Repeat.Range, Repeat, Empty
Extension methods. This is a special syntax form in C#. We can call a method on a variable like it is an instance method. But it is not on the class. It is specified in a separate place.Extension
Notes, syntactic sugar. LINQ can be seen as a form of syntactic sugar. Queries could be written with for-loops. But when a program is easier to read and write, often it is better.
Notes, queries. In query languages, we express what we want, not how it is to happen. The query language, not the programmer, is concerned with the exact implementation details.Quote: We call this language the query language, because it is very useful for retrieving information from data bases by formulating queries, or questions, expressed in the language (Structure and Interpretation of Computer Programs).
A summary. LINQ is a powerful feature. Its methods and query expressions often improve the readability of programs. And they sometimes lead to new, delayed, superior algorithms.
© TheDeveloperBlog.com