First, Grouping (group)
Example 1: Grouping 0-9 numbers by 3 evenly divisible results
Code:
1 Static voidLinq1 ()2 {3 int[] numbers = {5,3,2,4,0,7,8,6,9,1 };4 varNumModBy3 = fromNinchnumbers5Group N by n%36 Into G7 Select New{remainder = G.key, Numbers =g};8 9 foreach(varGinchnumModBy3)Ten { OneConsole.Write ("is divisible by 3 by the remainder {0}:", G.remainder); A foreach(varNuminchg.numbers) - { -Console.Write ("{0}", num); the } - Console.WriteLine (); - } -}
View Code
Example 2: Grouping words by first letter
Code:
1 Static voidLinq2 ()2 {3 string[] Words = {"Blueberry","Chimpanzee","Abacus","Banana","Apple","Cheese" };4 varGroupeddata = fromWinchwords5Group W by w[0]6 Into G7 byG.key//Add Sort8 Select New{Firstchar = G.key, Words =g};9 foreach(varGinchgroupeddata)Ten { OneConsole.Write ("starting with the {0} letter are:", G.firstchar); A foreach(varWordinchg.words) - { -Console.Write ("{0}", word); the } - Console.WriteLine (); - } -}
View Code
Second, set (set)
Distinct to Heavy
Source. Distinct ();
Other unions, Intersect, Except see LINQ 101 Sample
Iii. Conversion (conversion)
ToList
ToDictionary
Iv. Element (Elemental operation)
First
FirstOrDefault
LINQ 101--Grouping, Set, transform, Element