操作符:First
原型:2种
public static TSource First<TSource>( this IEnumerable<TSource> source )
public static TSource First<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate )
描述:First返回第一个元素,原型二返回满足特定条件的第一个元素。
int[] numSequences = new int[] { 32, 323, 45, 58, 90, 82, 43, 12 }; var firstOddNum = numSequences.OrderBy(num=>num).First(num => num % 2 == 1); Console.WriteLine(firstOddNum);
上面的例子中,我们将整型数据排序后,取出第一个奇数。
操作符:FirstOrDefault
原型:
public static TSource FirstOrDefault<TSource>( this IEnumerable<TSource> source )
public static TSource FirstOrDefault<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate )
描述:该操作符跟First类似,只是当我们没有找到满足条件的元素时,才有对应元素类型的默认值作为返回值,如int型返回0,string型返回空
操作符:Last
原型:
public static TSource Last<TSource>( this IEnumerable<TSource> source )
public static TSource Last<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate )
描述:第一个跟最后一个,语法类似,使用方法也类似。
操作符:LastOrDefault
原型:
public static TSource LastOrDefault<TSource>( this IEnumerable<TSource> source )
public static TSource LastOrDefault<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate )
描述:与FirstOrDefault类似
操作符:Single
原型:
public static TSource Single<TSource>( this IEnumerable<TSource> source )
public static TSource Single<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate )
描述:返回序列中唯一的一个元素,如果没有返回,或者没有满足predicate为true的元素,或者返回的元素超过一个,则抛出异常。
List<Student> students = new List<Student> { new Student{Name = "Terry",Scores = new List<int>{98,88,93,75,82}}, new Student{Name = "Tina",Scores = new List<int>{85,99,87,93,97}}, new Student{Name = "Linda",Scores = new List<int>{57,100,83,89,92}}, new Student{Name = "Leon",Scores = new List<int>{100,98,79,77,84}}, new Student{Name = "Echo",Scores = new List<int>{79,80,97,55,88}} }; Student terry = students.Single(s => s.Name.Equals("terry", StringComparison.OrdinalIgnoreCase)); Console.WriteLine("Terry's scores:"); foreach (int score in terry.Scores) { Console.WriteLine(score); }
输出结果为:
Terry's scores:
98
88
93
75
82
操作符:SingleOrDefault
原型:
public static TSource SingleOrDefault<TSource>( this IEnumerable<TSource> source )
public static TSource SingleOrDefault<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate )
描述:与Single类似,允许没有匹配的元素,如果没有匹配的元素,返回对应类型的默认值。如果返回超过一个元素,则抛出异常。
操作符:ElementAt
原型:
public static TSource ElementAt<TSource>( this IEnumerable<TSource> source, int index )
描述:返回指定索引位置的元素。如果输入序列实现了IList<T>,则可以直接根据索引查找,如果没有实现IList<T>,则需要遍历,直到索引位置。
操作符:ElementAtOrDefault
原型:
public static TSource ElementAtOrDefault<TSource>( this IEnumerable<TSource> source, int index )
描述:与ElementAt类似。
操作符:Any
原型:
public static bool Any<TSource>( this IEnumerable<TSource> source )
public static bool Any<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate )
描述:原型一中,只有输入序列含有元素,则返回true,这个原型事实上用处不大,因为我们可以用Count或Length来判断;原型二用来判断,输入序列是否包含满足使predicate为true的元素。
操作符:All
原型:
public static bool All<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate )
描述:Any是只要包含就为true,All需要所有的元素都满足条件才返回true
操作符:Contains
原型:
public static bool Contains<TSource>( this IEnumerable<TSource> source, TSource value )
public static bool Contains<TSource>( this IEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer )
描述:判断输入序列是否包含指定的元素,对于string类型,是区分大小写的。
string[] allBrands = new string[] { "Exuviance", "Avene", "Baby Quasar", "Ecoya", "Alterna", "Ecru New York" }; bool hasAvene = allBrands.Contains("avene",StringComparer.OrdinalIgnoreCase); Console.WriteLine(hasAvene);