LINQ.` In the island heat a bird calls out. The sound is clear and loud. This is a bird song. The animal queries for a potential mate.`In a query,` we ask for something. A bird asks for a mate. We ask for an array to be filtered or sorted or changed. LINQ is Language Integrated Query.`Average example.` LINQ introduces many extension methods. We use one (Average) to average all the elements in an int array. A double value is returned. `The Average method is implemented as an extension method within the .NET Framework. Extension methods have special syntax.`Important: `We include the System.Linq namespace with a "using" statement at the top. The program will not compile if we omit this.`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 `extension`Linq: `Extension methods are not part of LINQ. But they are essential in the understanding of this feature.`Things like "orderby" are transformed into extension method calls like OrderBy that act on sequences IEnumerables.`IEnumerable `ienumerable`Convert.` Some extension methods in LINQ convert from an IEnumerable to another type. They convert to an array, Dictionary, List or Lookup. `ToArray `toarray`ToDictionary `todictionary`ToList `tolist`ToLookup `tolookup`Mutate.` These methods filter or mutate. They change the elements in your query in some way. We remove unneeded elements, add new ones, or change other aspects of the elements themselves. `AsEnumerable `asenumerable`AsParallel `asparallel`AsQueryable `queryable`Cast `cast-extension`Concat `concat`Contains `contains-extension`DefaultIfEmpty `defaultifempty`Distinct `distinct`ElementAt `elementatordefault`ElementAtOrDefault `elementatordefault`Except `except`First `first`FirstOrDefault `firstordefault`GroupBy `groupby`GroupJoin `groupjoin`Intersect `intersect`Join `join`Last `lastordefault`LastOrDefault `lastordefault`OfType `oftype`OrderBy `orderby-extension`OrderByDescending `orderbydescending`Reverse `reverse`Select `select`SelectMany `selectmany`Single `single`SingleOrDefault `singleordefault`Union `union`Where `where`Zip `zip`Skip and take.` These extension methods are useful. They eliminate the need for custom code to check ranges. Skip passes over the first elements. `Skip, SkipWhile `skip`Take, TakeWhile `take`Computation.` LINQ also provides computational methods. These act upon a certain query and then return a number or other value. These can also simplify code. `Aggregate `aggregate`All `all`Any `any`Average `average`Count `count`SequenceEqual `sequenceequal`Sum `sum`Max and min.` We can search a collection for its largest (max) or smallest (min) value. This is effective for many value types. Which tower is the tallest? `Max, Min `max`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. `Empty: `The Empty method returns an empty enumerable collection. This can be useful as a "dummy" value.`Empty `empty`Range: `The Range method provides an enumerable collection that progresses from one value to another.`Range `enumerable-range`Repeat: `This method is repetitive—that is why it is called Repeat. It creates an enumerable collection full of one element.`Repeat `repeat`Query.` A query expression uses declarative clauses. These specify the results we want, not how we are to achieve them. To start, we use a query expression on an array of integers. `Imperative: `We describe how to accomplish the task by indicating each step in code statements.`Declarative: `We describe the final result needed, leaving the steps up to the query language.`In the query,` we select elements from an array in descending order (high to low). We filter out elements <= 2. In the loop, we evaluate the expression and print the results. `Var `var`Keywords.` Query expressions use a whole new set of keywords. These are contextual keywords. This means they only have meaning in query expressions. `ascending `descending`descending `descending`group `group`join `join`let `let`orderby `orderby`select new `select-new`LINQ versus loop.` LINQ has a performance cost. For small numeric operations, it introduces considerable overhead. For larger operations, this change is less important. `Benchmark: `We count all elements in an array greater than or equal to 10 with LINQ and with a for-loop.`Result: `The LINQ version is nearly ten times slower. For hot numeric loops, LINQ is a poor choice.`An error.` For queries to compile in the C# compiler we must include System.Linq at the top of the file. This is not optional. Visual Studio will provide a helpful error message. `Please add "using System.Linq" to fix this program. Compile-time errors are there to help us not make us mad.`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.`Books.` 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: MIT `https://mitpress.mit.edu/sicp/full-text/book/book.html`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.

OHYGXYYHDHQ;YHDXHQ.LinqXHbHJHAY{YOH%O{YOOXHz[]X HUHy{ 1, 3, 5, 7 };YOOH'HU.XAverageX());YO}Y}YYXYY4XYYH?HJExtensionMHgsY{YOHBH?HiXMultiplyByTwoX(XthisX HiHh)YO{XYOOH{This extension mHg multiplies an Hiby 2.YOOXHK Hh * X2X;YO}Y}YYHJHAY{YOH%O{XYOOH{Use our extension mHg.YOOXHQ.H'X4X.XMultiplyByTwoX());YO}Y}YYXYY8XYYHDHQ;YHDXHQ.LinqXHbHJHAY{YOH%O{YOOHz[] HUHy{ 1, 2, 3, 6, 7, 8 };XYOOH{Query expression.YOOXvar H8sHyXfromX H8HpHUYOOOOO orderby H8 descendingYOOOOO where H8 > 2YOOOOO select H8;XYOOH{Enumerate.YOOXH@ (var H8HpH8s)YOO{YOOOH5.Ha(H8);YOOOH5.Ha(' ');YOO}YOOH');YO}Y}YYXYY8 7 6 3XYYHDHQ;YH!HDHQ.LinqHbHJHAY{YOconst Hi_maxHy1000000;YOH%O{YOOHz[] HhsHy{ 10, 0, 1, 1, 20, 300, 400, 4 };XYYOOH{Version 1: use LINQ.YOOXvar s1HyH,.H`New();YOOHw(HiiHy0; i < _max; i++)YOO{YOOOHicountHyXH]LinqX(Hhs);YOO}YOOs1H3;XYYOOH{Version 2: use H|-loop.YOOXvar s2HyH,.H`New();YOOHw(HiiHy0; i < _max; i++)YOO{YOOOHicountHyXH]ForX(Hhs);YOO}YOOs2H3;YOOH'(H0(s1.H# * 1000000) /YOOO_max).ToHO(G0.00 nsG));YOOH'(H0(s2.H# * 1000000) /YOOO_max).ToHO(G0.00 nsG));YOOH5.Hu();YO}YYOH?HiXH]LinqX(Hz[] Hhs)YO{XYOOH{H] Hhs gH_r than or equalHj10 with LINQ.YOOXHK (from xHpHhsYOOOOwhere x >= 10YOOOOselect x).H]();YO}YYOH?HiXH]ForX(Hz[] Hhs)YO{XYOOH{H] Hhs gH_r than or equalHj10 with a loop.YOOXHicountHy0;YOOHw(HiiHy0; i < Hhs.LH^; i++)YOO{YOOOHmHhs[i] >= 10)YOOO{YOOOOcount++;YOOO}YOO}YOOHK count;YO}Y}YYXResultsXYYX111.83 nsX:OLINQ expression, H]()YX 10.86 nsX:OFor-loop, ifXYYHJHAY{YOH%O{YOOXHz[]X HEsHy{ 10, 20, 30 };XYOOH{We needHjinclude HQ.Linq HwthisHjwork.YOOXvar HMHyfrom nHpXHEsXYOOOOO select n;YO}Y}YYXResultsXYYError CS1935YCould not find an implementation of the query patternYHwsource type 'Hz[]'.Y'Select' not found.YAre you missing a referenceHj'HQ.Core.dll'X

%^``98LINQ extensionextension methodquery expressionbenchmarks LINQ, for-loopcauses System.Linq error