C#linq查询方法使用简介
2021-07-10 11:05
阅读:445
标签:each ica 集合 current elements 求和 spel cond data
All():判断所有元素是否都满足条件,若有一个不满足就返回false,否则返回true,源代码如下,如果方法里参数均为null,则会抛出异常。若该对象为null也会抛出异常,若该IEnumbe类型里元素个数为0,则会一直返回true
public static bool All(this IEnumerable source, Func 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 false; } return true; }
Any():如下判断对象里是否有元素,若集合为null,则会报错public
static bool Any(this IEnumerable source) { if (source == null) throw Error.ArgumentNull("source"); using (IEnumerator e = source.GetEnumerator()) { if (e.MoveNext()) return true; } return false; }
如下判断对象里是否存在满足某条件的,若满足为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 IEnumerablesource) { if (source == null) throw Error.ArgumentNull("source"); long sum = 0; long count = 0; checked { foreach (int v in source) { sum += v; count++; } } if (count > 0) return (double)sum / count; throw Error.NoElements(); }
public static double? Average(this IEnumerablesource) { if (source == null) throw Error.ArgumentNull("source"); long sum = 0; long count = 0; checked { foreach (int? v in source) { if (v != null) { sum += v.GetValueOrDefault(); count++; } } } if (count > 0) return (double)sum / count; return null; }
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(this IEnumerable source, TSource value) { ICollection collection = source as ICollection ; if (collection != null) return collection.Contains(value); return Contains (source, value, null); } public static bool Contains (this IEnumerable source, TSource value, IEqualityComparer comparer) { if (comparer == null) comparer = EqualityComparer .Default; if (source == null) throw Error.ArgumentNull("source"); foreach (TSource element in source) if (comparer.Equals(element, value)) return true; return false; }
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求和
C#linq查询方法使用简介
标签:each ica 集合 current elements 求和 spel cond data
原文地址:https://www.cnblogs.com/LGDD/p/9688479.html
评论
亲,登录后才可以留言!