Cast
Cast()将非泛型序列,转换为泛型序列。
ArrayList arrayList1 = new ArrayList() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
IEnumerable<int> tList = arrayList1.Cast<int>();
运行结果:
OfType
OfType()提取出集合中符合的指定类型并生成新的集合。
ArrayList arrayList2 = new ArrayList() { 1, 2, 3, 4, 5, 6, 7, 8, "dasdas" };
var tList2 = arrayList2.OfType<int>();
运行结果:
Intersect
取出两个集合中相同的元素
string[] words1 = { "a1", "b2", "c3", "d4" };
string[] words2 = { "a", "b2", "c3", "d" };
var arr1 = words1.Intersect(words2);
运行结果:
Except
去出两个集合中相同的元素 保留第一个集合中的不同的元素
string[] words1 = { "a1", "b2", "c3", "d4" };
string[] words2 = { "a", "b2", "c3", "d" };
var arr2 = words1.Except(words2);
运行结果:
Union
合并两个集合 去重
string[] words1 = { "a1", "b2", "c3", "d4" };
string[] words2 = { "a", "b2", "c3", "d" };
var arr3 = words1.Union(words2);
运行结果:
Concat
将两个集合连接
string[] words1 = { "a1", "b2", "c3", "d4" };
string[] words2 = { "a", "b2", "c3", "d" };
var arr4 = words1.Concat(words2);
运行结果: