static void Main(string[] args) { string[] words = { "zero", "one", "two", "three", "four" }; int[] numbers = { 0, 1, 2, 3, 4 }; string[] names = { "Robin", "Ruth", "Bob", "Emma" }; string[] colors = { "Red", "Blue", "Beige", "Green" }; string[] abbc = { "a", "b", "b", "c" }; string[] cd = { "c", "d" }; #region 聚合 立即執(zhí)行 //Console.WriteLine(numbers.Sum());//10 //Console.WriteLine(numbers.Count());//5 //Console.WriteLine(numbers.Average());//2 //Console.WriteLine(numbers.LongCount(c => c % 2 == 0));//3 //Console.WriteLine(words.Min(word => word.Length));//3 //Console.WriteLine(words.Max(word => word.Length));//5 //Console.WriteLine(numbers.Aggregate("seed", (current, item) => current + item, result => result.ToUpper()));//SEED01234 #endregion #region 連接 延遲執(zhí)行 流式數(shù)據(jù) //var result = numbers.Concat(new int[] { 5, 6, 7, 8, 9 }); //foreach (var i in result) //{ // Console.WriteLine(i);//0,1,2,3,4,5,6,7,8,9 //} #endregion #region 轉(zhuǎn)換 object[] allStrings = { "these", "are", "all", "strings" }; object[] notAllStrings = { "number", "at", "the", "end", 5 }; ////延遲執(zhí)行 流式數(shù)據(jù) ////allStrings.Cast<string>();//強轉(zhuǎn),生成 IEnumerable<string> "these", "are", "all", "strings",轉(zhuǎn)換失敗則拋出異常 ////allStrings.OfType<string>();//嘗試轉(zhuǎn)換,生成 IEnumerable<string> "these", "are", "all", "strings", 過濾掉轉(zhuǎn)換失敗的元素 ////notAllStrings.Cast<string>(); ////notAllStrings.OfType<string>(); ////立即執(zhí)行 //numbers.ToArray(); //numbers.ToList(); //words.ToDictionary(s => s.Substring(0, 2)).Print();//[ze,zero],[on,one].... //var result = words.ToLookup(word => word[0]); ///* // * z:zero // * o:one // * t:two,three // * f:four // * // */ //foreach (var item in result) //{ // Console.WriteLine(item.Key); // foreach (var s in item) // { // Console.WriteLine("--" + s); // } //} ////words.ToDictionary(word => word[0]);//拋出異常,每個鍵只能有一個元素,所以在遇到"t"時轉(zhuǎn)換失敗 #endregion #region 元素操作符 立即執(zhí)行 //Console.WriteLine(words.ElementAt(2)); //Console.WriteLine(words.ElementAtOrDefault(10)); //words.First(); //words.First(f => f.Length == 3); //words.FirstOrDefault(f => f.Length > 10); //words.Last(); //words.Single(); ////... #endregion #region 相等操作符 立即執(zhí)行 //var b1 = words.SequenceEqual(new string[] { "zero", "one", "two", "three", "four" });//true //var b2 = words.SequenceEqual(new string[] { "ZERO", "one", "Two", "three", "four" });//false //var b3 = words.SequenceEqual(new string[] { "Zero", "one", "Two", "three", "four" }, StringComparer.OrdinalIgnoreCase);//true //Console.WriteLine($"{b1},{b2},{b3}"); #endregion #region 生成 延遲執(zhí)行 流式處理 //numbers.DefaultIfEmpty();//{ 0, 1, 2, 3, 4 } //new int[0].DefaultIfEmpty();//{0} //new int[0].DefaultIfEmpty(100);//{100} //Enumerable.Range(0, 9);//{0,1,2,3,4,5,6,7,8} //Enumerable.Repeat(99, 3);//{99,99,99} //Enumerable.Empty<int>();//一個類型為 IEnumerable<int> 的空序列 #endregion #region 分組 延遲執(zhí)行 緩存數(shù)據(jù) 當?shù)纸M的結(jié)果序列時,消費的是整個輸入序列 //ToLookup算是其中一個 //words.GroupBy(word => word.Length); //var result = words.GroupBy(word => word.Length, word => word.ToUpper()); /* * 4:ZERO,FOUR * 3:ONE,TWO * 5:THREE */ //foreach (var item in result) //{ // Console.WriteLine(item.Key+":"); // item.Print(); //} //var result = words.GroupBy(word => word.Length, (key, g) => key + ":" + g.Count()); //foreach (var item in result) //{ // item.Print();//4:2,3:2,5:1 //} #endregion #region 連接 延遲執(zhí)行,流式處理左邊序列,對于右邊序列,在請求第一個結(jié)果時變讀取其全部內(nèi)容 //names.Join(colors, left => left[0], right => right[0], (left, right) => left + ":" + right).Print(); //Console.WriteLine("**************"); //names.GroupJoin(colors, left => left[0], right => right[0], (left, right) => left + ":" + string.Join(",", right)).Print(); #endregion #region 分部 延遲執(zhí)行 流式處理 //words.Skip(1).Take(2); //words.TakeWhile(word => word.Length <= 4); //words.SkipWhile(word => word.Length <= 4); #endregion #region 投影 延遲執(zhí)行 //words.Select(word => word.Length); //index 表示當前元素在原序列中的索引 //words.Select((word, index) => index + ":" + word).Print();//0:zero,1:one,2:two... //將序列中的每個元素都投影到一個新的序列中 //words.SelectMany(word => word.ToArray()).Print();//z,e,r,o,o,n,e,t,w,o,t,h,r,e,e,,f,o,u,r /* * one * two * two * three * three * three * four * four * four * four * */ //words.SelectMany((word, index) => Enumerable.Repeat(word, index)).Print(); //投影順序:先是每個序列的第一個元素,接著是每個序列的第二個元素,直到任意一個源序列到達末尾,結(jié)果序列都將停止生成 //names.Zip(colors.Take(3), (left, right) => left + ":" + right).Print(); #endregion #region 數(shù)量 立即執(zhí)行 words.All(word => word.Length > 2);//true words.All(word => word.Length > 6);//false words.Any();//true words.Any(word => word.Length == 6);//false words.Contains("ONE", StringComparer.OrdinalIgnoreCase);//true #endregion #region 過濾 延遲執(zhí)行 流式數(shù)據(jù) //OfType 也算過濾 //words.Where(word => word.Length > 3); //words.Where((word, index) => index < word.Length).Print();//four 的索引是 4,長度是 4,所以不會打印出來 #endregion #region 基于集的操作符 延遲執(zhí)行 //流式處理 //abbc.Distinct().Print();//a,b,c //四種操作都有該方式的重載 abbc.Distinct(StringComparer.OrdinalIgnoreCase).Print();//a,b,c //abbc.Union(cd).Print();//a,b,c,d //左序列流式處理,有序列緩存處理 //abbc.Intersect(cd).Print();//c //abbc.Except(cd).Print();//a,b #endregion #region 排序 延遲執(zhí)行 緩存處理 words.OrderBy(word => word.Length); words.OrderByDescending(word => word.Length); words.OrderBy(word => word.Length).ThenBy(word => word); words.Reverse(); #endregion Console.ReadKey(); }