在前面说到LINQ to Object——延时执行的Enumerable类的方法,接下来说说LINQ to Object——立即执行的Enumerable类方法。
1.ToArray 序列转换成数组
List<string> names_list = new List<string> { "张三", "范冰冰", "李冰冰", "迈克尔·杰克逊", "李四", "王五", "赵六", "田七" }; string[] takenames_arry = names_list.ToArray(); string[] takenames_arry2 = (from name in names_list select name).Take(4).ToArray(); foreach (var name in takenames_arry2) { Console.WriteLine(name); } Console.WriteLine("----------------------------"); Console.ReadKey();
运行结果:
2.ToList 序列转换成List<T>
string[] names_array = { "张三", "范冰冰", "李冰冰", "迈克尔·杰克逊", "李四", "王五", "赵六", "田七" }; List<string> takenames_list = names_array.ToList(); List<string> takenames_list2 = (from name in names_array select name).Skip(4).ToList(); foreach (var name in takenames_list2) { Console.WriteLine(name); } Console.WriteLine("----------------------------"); Console.ReadKey();
运行结果:
3.ToDictionary 把序列转换为泛型Dictionary<TKey,TValue>
4.ToLookup 用于将序列转换为泛型Lookup<TKey,TValue>
5.SequenceEqual 比较两个序列是否相等
bool sequence_equal = names_array.SequenceEqual(names_list); bool sequence_equal2 = names_array.Skip(1).Take(2).SequenceEqual(names_list.Take(3).SkipWhile(n => n.Length == 2)); Console.WriteLine("{0},{1}", sequence_equal, sequence_equal2); Console.WriteLine("----------------------------"); Console.ReadKey();
运行结果:
6.First 返回序列第一个满足条件元素
var first_name = names_array.First(); var first_name2 = names_array.First(n => n.Length == 3); Console.WriteLine("{0},{1}", first_name, first_name2); Console.WriteLine("----------------------------"); Console.ReadKey();
运行结果:
7.FirstOrDefault 返回序列第一个满足条件元素,如果没有找到则返回默认值
var first_or_default_name = names_array.FirstOrDefault(); var first_or_default_name2 = names_array.FirstOrDefault(n => n == "123"); Console.WriteLine("{0},{1}", first_or_default_name, first_or_default_name2); Console.WriteLine("----------------------------"); Console.ReadKey();
运行结果:
8.Last 返回序列最后一个满足条件元素
var last_name = names_array.Last(); var last_name2 = names_array.LastOrDefault(n => n.Length == 3); Console.WriteLine("{0},{1}", last_name, last_name2); Console.WriteLine("----------------------------"); Console.ReadKey();
运行结果:
9.LastOrDefault 返回序列最后一个满足条件元素,如果没有找到则返回默认值
var last_or_default_name = names_array.LastOrDefault(); var last_or_default_name2 = names_array.LastOrDefault(n => n == "123"); Console.WriteLine("{0},{1}", last_or_default_name, last_or_default_name2); Console.WriteLine("----------------------------"); Console.ReadKey();
运行结果:
10.Single 返回序列中唯一的元素,注意:如果序列中包含多个元素,会引发运行错误
try { var single_name = names_array.Single(); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine("----------------------------"); var single_name2 = names_array.Single(n => n == "张三"); Console.WriteLine("{0}", single_name2); Console.WriteLine("----------------------------"); Console.ReadKey();
运行结果:
11.SingleOrDefault 找出序列中满足一定条件的元素,注意:如果序列为空则返回默认值, 如果序列中包含多个多个元素会引发运行错误
try { var single_or_default_name = Enumerable.Empty<int>().SingleOrDefault(); Console.WriteLine("{0}", single_or_default_name);//不报错,如果序列为空就返回默认值 } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine("----------------------------"); try { var single_or_default_name2 = names_array.SingleOrDefault(); Console.WriteLine("{0}", single_or_default_name2);//报错,序列包含多行错误 } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine("----------------------------"); var single_or_default_name3 = Enumerable.Empty<string>().DefaultIfEmpty("默认值").SingleOrDefault(); Console.WriteLine("{0}", single_or_default_name3); Console.WriteLine("----------------------------"); var single_or_default_name4 = names_array.SingleOrDefault(n => n == "123"); Console.WriteLine("{0}", single_or_default_name4); Console.WriteLine("----------------------------"); Console.ReadKey();
运行结果:
12.ElementAt 获得指定索引处的元素
var element_at_name = names_array.ElementAt(3); Console.WriteLine("{0}", element_at_name); Console.WriteLine("----------------------------"); Console.ReadKey();
运行结果:
13.ElementAtOrDefault 获得指定索引处的元素,如果超出索引,则返回元素类型的默认值
var element_at_or_default_name = names_array.ElementAtOrDefault(5); var element_at_or_default_name2 = names_array.ElementAtOrDefault(10); Console.WriteLine("{0},{1}", element_at_or_default_name, element_at_or_default_name2); Console.WriteLine("----------------------------"); Console.ReadKey();
运行结果:
14.All 序列中的所有元素是否都满足条件
15.Any 序列中的元素是否存在或满足条件
16.Contains 确定元素是否在序列中
17.Count 序列包含元素的数量
18.LongCount 获取一个Int64类型的元素数量
19.Aggregate 将序列元素进行累加
20.Sum 序列之和
21.Average 序列平均值
22.Min 序列的最小值
23.Max 序列的最大值
未完待续。。。。。。
原文地址:https://www.cnblogs.com/jiechou/p/9226494.html