C#linq查询方法使用简介
2021-07-10 11:05
标签:each ica 集合 current elements 求和 spel cond data public 满足条件的第一个元素 C#linq查询方法使用简介 标签:each ica 集合 current elements 求和 spel cond data 原文地址:https://www.cnblogs.com/LGDD/p/9688479.htmlAll():判断所有元素是否都满足条件,若有一个不满足就返回false,否则返回true,源代码如下,如果方法里参数均为null,则会抛出异常。若该对象为null也会抛出异常,若该IEnumbe类型里元素个数为0,则会一直返回true
public static bool All
Any():如下判断对象里是否有元素,若集合为null,则会报错
如下判断对象里是否存在满足某条件的,若满足为true,否则为false 若对象为null,则会报错 public static bool AnyTSource>(this IEnumerableTSource> source, FuncTSource, bool> predicate) {
if (source == null) throw Error.ArgumentNull("source");
if (predicate == null) throw Error.ArgumentNull("predicate");
foreach (TSource element in source) {
if (predicate(element)) return true;
}
return false;
}
Average 求平均值 若集合为Null,则会抛出异常,若集合中没有元素,则也会抛出异常
public static double Average(this IEnumerable
public static double? Average(this IEnumerable
LastOrDefault/FirstOrDefault 返回第一个元素,若没有元素,则返回元素的默认值,若集合为Null,则抛出异常,LastOrDefault 相反
First/Last 返回第一个或者最后一个元素,若集合为中没有元素,则抛出异常
Single/SingleOrDefault 返回序列的唯一元素,若存在多个元素,则都报错,若集合为null,则 SingleOrDefault 返回默认值
public static TSource FirstOrDefaultTSource>(this IEnumerableTSource> source) {
if (source == null) throw Error.ArgumentNull("source");
IListTSource> list = source as IListTSource>;
if (list != null) {
if (list.Count > 0) return list[0];
}
else {
using (IEnumeratorTSource> e = source.GetEnumerator()) {
if (e.MoveNext()) return e.Current;
}
}
return default(TSource);
}
public static TSource FirstOrDefaultTSource>(this IEnumerableTSource> source, FuncTSource, bool> predicate) {
if (source == null) throw Error.ArgumentNull("source");
if (predicate == null) throw Error.ArgumentNull("predicate");
foreach (TSource element in source) {
if (predicate(element)) return element;
}
return default(TSource);
}
ToList/toArray/ToDictionary
public static ListTSource> ToListTSource>(this IEnumerableTSource> source) {
if (source == null) throw Error.ArgumentNull("source");
return new ListTSource>(source);
}
LongCount/Count 返回元素个数,若集合为Null,则抛出异常
public static int CountTSource>(this IEnumerableTSource> source) {
if (source == null) throw Error.ArgumentNull("source");
ICollectionTSource> collectionoft = source as ICollectionTSource>;
if (collectionoft != null) return collectionoft.Count;
ICollection collection = source as ICollection;
if (collection != null) return collection.Count;
int count = 0;
using (IEnumeratorTSource> e = source.GetEnumerator()) {
checked {
while (e.MoveNext()) count++;
}
}
return count;
}
Contains
public static bool Contains
Concat
blic static IEnumerableTSource> ConcatTSource>(this IEnumerableTSource> first, IEnumerableTSource> second) {
if (first == null) throw Error.ArgumentNull("first");
if (second == null) throw Error.ArgumentNull("second");
return ConcatIteratorTSource>(first, second);
}
static IEnumerableTSource> ConcatIteratorTSource>(IEnumerableTSource> first, IEnumerableTSource> second) {
foreach (TSource element in first) yield return element;
foreach (TSource element in second) yield return element;
}
Distinct
public static IEnumerableTSource> DistinctTSource>(this IEnumerableTSource> source) {
if (source == null) throw Error.ArgumentNull("source");
return DistinctIteratorTSource>(source, null);
}
public static IEnumerableTSource> DistinctTSource>(this IEnumerableTSource> source, IEqualityComparerTSource> comparer) {
if (source == null) throw Error.ArgumentNull("source");
return DistinctIteratorTSource>(source, comparer);
}
static IEnumerableTSource> DistinctIteratorTSource>(IEnumerableTSource> source, IEqualityComparerTSource> comparer) {
SetTSource> set = new SetTSource>(comparer);
foreach (TSource element in source)
if (set.Add(element)) yield return element;
}
ElementAt /ElementAtOrDefault 返回某个索引处的元素,第一个若索引不存在,则抛出异常,第二个方法若索引越界,则返回元素的默认值。
Except 返回集合差集 Union 返回集合并集 Intersect返回交集
Max/Min 返回集合的最大值和最小值 Sum求和