@bBfCrBXWC| 557VB~CB 7564647VYYZYYYYYYYYYYYYYYBZBZYYYZ-~B~B~B-~~Z 7656VYYYB-~C 99796F55W 564FX~-

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.`In a query,` we ask for something. We ask for an array to be filtered or sorted or changed. LINQ is Language Integrated Query. System.Linq is useful in many programs.`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).`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.

GHJ HPHO; HPJHO.LinqJHSHDH; { GH$G{ GGJH}[]J HTHz{ 1, 3, 5, 7 }; GGH%HT.JAverageJ()); G} } J 4J HAHDExtensionMH[s { GH=HAHsJMultiplyByTwoJ(JthisJ HsHd) G{J GGH{This extension mH[ multiplies an Hsby 2. GGJHJ Hd * J2J; G} } HDH; { GH$G{J GGH{Use our extension mH[. GGJHO.H%J4J.JMultiplyByTwoJ()); G} } J 8J HPHO; HPJHO.LinqJHSHDH; { GH$G{ GGH}[] HTHz{ 1, 2, 3, 6, 7, 8 };J GGH{Query expression. GGJvar H6sHzJfromJ H6HkHTH'GG orderby H6 descendingH'GG where H6 > 2H'GG select H6;J GGH{Enumerate. GGJH7 (var H6HkH6s) GG{H'H4.Hh(H6);H'H4.Hh(' '); GG} GGH%); G} } J 8 7 6 3J HPHO; H!HPHO.LinqHSHDH; { Gconst Hs_maxHz1000000; GH$G{ GGH}[] HdsHz{ 10, 0, 1, 1, 20, 300, 400, 4 };J GGH{Version 1: use LINQ. GGJvar s1HzH-.HcNew(); GGHo(HsiHz0; i < _max; i++) GG{H'HscountHzJHWLinqJ(Hds); GG} GGs1H1;J GGH{Version 2: use H|-loop. GGJvar s2HzH-.HcNew(); GGHo(HsiHz0; i < _max; i++) GG{H'HscountHzJHWForJ(Hds); GG} GGs2H1; GGH%(H.(s1.H" * 1000000) /H'_max).ToHM("0.00 ns")); GGH%(H.(s2.H" * 1000000) /H'_max).ToHM("0.00 ns")); GGH4.Hx(); G} GHAHsJHWLinqJ(H}[] Hds) G{J GGH{HW Hds gH`r than or equalHl10 with LINQ. GGJHJ (from xHkHdsH'Gwhere x >= 10H'Gselect x).HW(); G} GHAHsJHWForJ(H}[] Hds) G{J GGH{HW Hds gH`r than or equalHl10 with a loop. GGJHscountHz0; GGHo(HsiHz0; i < Hds.LHZ; i++) GG{H'HrHds[i] >= 10)H'{H'Gcount++;H'} GG} GGHJ count; G} } JResultsJ J111.83 nsJ:GLINQ expression, HW() J 10.86 nsJ:GFor-loop, ifJ HDH; { GH$G{ GGJH}[]J HGsHz{ 10, 20, 30 };J GGH{We needHlinclude HO.Linq HothisHlwork. GGJvar HIHzfrom nHkJHGsJH'GG select n; G} } JResultsJ Error CS1935 Could not find an implementation of the query pattern Hosource type 'H}[]'. 'Select' not found. Are you missing a referenceHl'HO.Core.dll'J

^LINQ extension`extension method`query expression9benchmarks LINQ, for-loop8causes System.Linq error