续上篇LINQ入门教程之各种标准查询操作符(一)

        LINQ入门教程之各种标准查询操作符(二)

7.  聚合操作符

View Code

 

8.  集合操作符

View Code

 

9.  生成操作符

 1 #region   生成操作符 即从现有序列的值中创建新的序列  
 2         /// <summary>
 3         /// 返回一个指定类型的空集
 4         /// </summary>
 5         static void EmptyFunction()
 6         {
 7             string[] name1 = { "1","2" };
 8             string[] name2={"3","4","5"};
 9             string[] name3 = { "3", "7", "8", "9" };
10             List<string[]> strlists=new List<string[]>{name1,name2,name3};
11             //无符合条件,返回空集合
12             IEnumerable<string> nameslist = strlists.Aggregate(Enumerable.Empty<string>( ),
13                 (current, next) => next.Length > 5? current.Union(next) : current);
14            
15             foreach (string item in nameslist)
16             {
17                 Console.WriteLine(item);
18             }
19         }
20         /// <summary>
21         /// 创建一个包含数字序列的集合 包含两个参数,第一个参数是作为序列开始的整数值,第二个参数是要产生的整数序列中数字的个数
22         /// </summary>
23         static void RangeFunction()
24         {
25             IEnumerable<int> intAry = Enumerable.Range(2, 5);
26             foreach (int item in intAry)
27             {
28                 Console.Write(item + " ");
29             }
30         }//output: 2 3 4 5 6
31         /// <summary>
32         /// Repeat 操作符创建一个单值序列,将此值重复一定的次数
33         /// </summary>
34         static void RepeatFunction()
35         {
36             IEnumerable<string[]> strAry = Enumerable.Repeat(new string[]{"world","hello"}, 2);
37             foreach (string[] item in strAry)
38             {
39                 Console.WriteLine(item[1] +" "+item[0] );
40             }
41         } 
42         //outpupt: 
43         //hello world
44         //hello world
45         #endregion
View Code

相关文章:

  • 2021-09-05
  • 2021-12-11
  • 2021-08-17
  • 2022-12-23
  • 2021-12-24
猜你喜欢
  • 2021-07-13
  • 2021-10-06
  • 2022-12-23
  • 2021-05-02
相关资源
相似解决方案