[微软] Enumerable 类
[微软] IEnumerable 接口
[微软] IEnumerable<T> 接口
需要引入的命名空间
//IEnumerable
using System.Collections;
//IEnumerable<TSource>
using System.Collections.Generic;
//扩展方法都在Linq命名空间
using System.Linq;
| IEnumerable<TSource> | |
| 扩展方法 | 描述 |
| Intersect() | 返回两个集合的交集。示例1 |
| Where() | 返回满足条件的集合。示例2 |
| Select() | 对集合中的每个元素进行转换后输出。示例3 |
| SelectMany() | 遍历嵌套集合。示例4 |
| Take() | 返回集合的前n个元素。示例5 |
| TakeWhile() | 根据条件判断返回元素。示例6 |
| Skip() | 跳过集合中指定数量的元素,并返回剩余的元素。示例7 |
| SkipWhile() | 按条件跳过集合中的元素,并返回剩余的元素。示例8 |
| Join()、GroupJoin() | 匹配两个集合中键相等的元素,经转换后返回。示例9 |
| Where() | 按条件搜索并返回新的集合。示例10 |
int[] arr1 = { 0, 2, 3, 4 };
int[] arr2 = { 1, 2, 3, 5 };
//返回两个集合的交集
IEnumerable<int> arr = arr1.Intersect<int>(arr2);
foreach (int i in arr)
Console.WriteLine(i);//输出2,3
int[] arr1 = { 0, 2, 3, 4, 5, 6, 7 };
//返回满足查询条件的集合
IEnumerable<int> arr = arr1.Where<int>(x => x > 5);
foreach (int i in arr)
Console.WriteLine(i);//输出6,7
string[] arr1 = { "A", "B", "C" };
//对集合中的每个元素进行转换后输出
IEnumerable<string> arr = arr1.Select((x,index) => (x + index).ToString());
foreach (string str in arr)
{
//输出 A1 B2 C3
Console.Write(str + " ");
}
int[] arr1 = { 1, 2 };
string[] arr2 = { "A", "B", "C" };
var arr = arr1.SelectMany(num=>arr2, (num, letter)=>new { num, letter });
foreach (var x in arr)
{
Console.WriteLine(x.num + ":" + x.letter);
}
//上面的代码等效于下面的代码
for(int i = 0; i < arr1.Length; i++)
{
for(int j = 0; j < arr2.Length; j++)
Console.WriteLine(arr1[i] + ":" + arr2[j]);
}
//end
int[] arr1 = { 1, 2, 3, 4, 5 };
//返回集合的前3个元素
IEnumerable<int> arr = arr1.Take<int>(3);
foreach (int i in arr)
Console.WriteLine(i);//输出1,2,3
int[] arr1 = { 1, 2, 3, 4, 5 };
//返回集合中元素索引小于3的元素
//注意:此API不会遍历所有元素,一旦条件为false就停止遍历并返回
IEnumerable<int> arr = arr1.TakeWhile<int>((num, index)=>index<3);
//不会返回任何元素,因为第一次遍历时条件判断就返回false
//IEnumerable<int> arr = arr1.TakeWhile<int>((num, index)=>index>3);
foreach (int i in arr)
Console.WriteLine(i);//输出1,2,3
int[] arr1 = { 1, 2, 3, 4, 5 };
//跳过集合中指定数量的元素,返回剩余的元素
IEnumerable<int> arr = arr1.Skip<int>(3);
foreach (int i in arr)
Console.WriteLine(i);//输出4,5
int[] arr1 = { 1, 2, 3, 4, 5 };
//跳过小于3的元素
//注意:此API不会遍历所有元素,一但条件为false就停止遍历并返回剩余的元素
IEnumerable<int> arr = arr1.SkipWhile<int>((num, index)=>num<3);
//返回全部元素,因为第一次遍历时条件判断就返回false
//IEnumerable<int> arr = arr1.SkipWhile<int>((num, index)=>num>3);
foreach (int i in arr)
Console.WriteLine(i);//输出3,4,5
public class KeyValue
{
public string key;
public string value;
}
List<KeyValue> list1 = new List<KeyValue>();
List<KeyValue> list2 = new List<KeyValue>();
list1.Add(new KeyValue() { key = "K1" });
list1.Add(new KeyValue() { key = "K2" });
list2.Add(new KeyValue() { key = "K2" });
list2.Add(new KeyValue() { key = "K3" });
//返回list1与list2中key相等的元素,并传给转换函数(第三个参数)
var arr = list1.Join(list2, x=>x.key, y=>y.key, (x, y)=>x.key+"-"+y.key);
//第三个参数中的y是一个y[i].key==x.key的集合
//var arr = list1.GroupJoin(list2, x => x.key, y => y.key, (x, y) => x.key + "-"+y.Count());
foreach (string s in arr)
Console.WriteLine(s);//输出k2-k2
int[] arr1 = { 1, 2, 3, 4, 5 };
var arr = arr1.Where(x=>x>3);
foreach (int i in arr)
Console.WriteLine(i);//输出4,5