Today, there is not much speech, only code, extension methods too much, can not be listed, some books, see more will be used.
1 //Enumerable.range Returns the start to end range, which is a enumrable<int> type2 //The range method does not really construct a list with the appropriate numbers, it only generates those numbers at the right time, "just in"3 varC0 = Enumerable.range (1,Ten);4 foreach(varEinchC0)5 {6Console.WriteLine (e);//Print 1,2,3,4,5,6,7,8,9,107 }8 9 //Reverse (), reverse listTen varC1 =C0. Reverse (); One foreach(varEinchC1) A { -Console.WriteLine (e);//Print 10,9,8,7,6,5,4,3,2,1 - } the - //where (), filtering - varC2 = C1. Where (x = x%2==0);//returns the element that conforms to the predicate condition, the return type is enumrable<int> - foreach(varEinchC2) + { -Console.WriteLine (e);//10,8,6,4,2 + } A atConsole.WriteLine ("---------------------------------------------"); - - //use chained operations, because each operation returns enumrable<int> -C0. Reverse (). Where (x = x%2==0). ToList (). ForEach (x =Console.WriteLine (x)); - - //Select (), drop shadow, return to List of custom type objects in string[] Arrs = {"James","John","Michelle","Amy","Kim" }; - to //Returns a list of anonymous types that contain the first and last attributes + varOBJS = Arrs. Select (x =New{first = X.first (), last =x.last ()}); - foreach(varEinchObjs) the { *Console.WriteLine (E.first);//j,j,m,a,k $Console.WriteLine (E.last);//s,n,e,y,mPanax Notoginseng } - the //Select the element that starts with "J" and move y from the first to the non- + varYOBJS = Arrs. ToList (). FindAll (x = X.startswith ("J")). Select (x = A { the vary = X.remove (0,1); + returnY +"J"; - }); $ foreach(varEinchYobjs) $ { -Console.WriteLine (e);//Amesj,ohnj - } the - //sort using order BY, return iorderedenumerable<string>Wuyi varOrderarrs = Arrs. (x =X.first ()); the foreach(varEinchOrderarrs) - { WuConsole.WriteLine (e);//Amy,james,john,kim,michelle - } About $ //ThenBy (), first sorted by First order (), and then in unsorted order - varThenarrs = Arrs. (x = X.first ()). ThenBy (x =x.last ()); - foreach(varEinchThenarrs) - { AConsole.WriteLine (e);//Amy,john,james,kim,michelle +}
Please treatise.
19.c# describes the extension methods in IEnumerable and Ienumerable<t> (10.3-10.5)