我喜欢绳子。IsNullOrEmpty方法。我想有一些东西,将允许相同的功能IEnumerable。有这样的东西吗?也许是一些收集助手类?我问的原因是,在if语句中,如果模式是(mylist != null && mylist. any()),代码看起来很混乱。使用Foo.IsAny(myList)会干净得多。
这篇文章没有给出答案:IEnumerable是空的?
我喜欢绳子。IsNullOrEmpty方法。我想有一些东西,将允许相同的功能IEnumerable。有这样的东西吗?也许是一些收集助手类?我问的原因是,在if语句中,如果模式是(mylist != null && mylist. any()),代码看起来很混乱。使用Foo.IsAny(myList)会干净得多。
这篇文章没有给出答案:IEnumerable是空的?
当前回答
我根据@Matt Greer的回答构建了这个
他完美地回答了那位警官的问题。
我想要这样的东西,同时保持Any的原始功能,同时还检查null。我张贴这个以防其他人需要类似的东西。
具体来说,我希望仍然能够传递一个谓词。
public static class Utilities
{
/// <summary>
/// Determines whether a sequence has a value and contains any elements.
/// </summary>
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
/// <param name="source">The <see cref="System.Collections.Generic.IEnumerable"/> to check for emptiness.</param>
/// <returns>true if the source sequence is not null and contains any elements; otherwise, false.</returns>
public static bool AnyNotNull<TSource>(this IEnumerable<TSource> source)
{
return source?.Any() == true;
}
/// <summary>
/// Determines whether a sequence has a value and any element of a sequence satisfies a condition.
/// </summary>
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
/// <param name="source">An <see cref="System.Collections.Generic.IEnumerable"/> whose elements to apply the predicate to.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <returns>true if the source sequence is not null and any elements in the source sequence pass the test in the specified predicate; otherwise, false.</returns>
public static bool AnyNotNull<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
return source?.Any(predicate) == true;
}
}
扩展方法的命名可能会更好。
其他回答
只需添加使用系统。Linq和看到魔术发生时,你试图访问可用的方法在IEnumerable。添加它将使您能够访问名为Count()的方法,就像这样简单。只要记住在调用count():)之前检查空值即可。
另一种方法是获取枚举器并调用MoveNext()方法来查看是否有任何项:
if (mylist != null && mylist.GetEnumerator().MoveNext())
{
// The list is not null or empty
}
这适用于IEnumerable和IEnumerable<T>。
我使用Bool IsCollectionNullOrEmpty = !(Collection?. any ()??false);。希望这能有所帮助。
分解:
. any()如果Collection为空将返回null,如果Collection为空则返回false。
收集? .Any () ?如果Collection为空,则返回false;如果Collection为空,则返回false。
它的补将得到IsEmptyOrNull。
public static bool IsNullOrEmpty<T>(this IEnumerable<T> enumerable) {
return enumerable == null || !enumerable.Any();
}
由于一些资源在一次读取后会耗尽,我想为什么不将检查和读取结合起来,而不是传统的单独检查,然后读取。
首先,我们有一个更简单的检查为空的内联扩展:
public static System.Collections.Generic.IEnumerable<T> ThrowOnNull<T>(this System.Collections.Generic.IEnumerable<T> source, string paramName = null) => source ?? throw new System.ArgumentNullException(paramName ?? nameof(source));
var first = source.ThrowOnNull().First();
然后我们有一些更复杂的(好吧,至少我写它的方式)检查为空和空的内联扩展:
public static System.Collections.Generic.IEnumerable<T> ThrowOnNullOrEmpty<T>(this System.Collections.Generic.IEnumerable<T> source, string paramName = null)
{
using (var e = source.ThrowOnNull(paramName).GetEnumerator())
{
if (!e.MoveNext())
{
throw new System.ArgumentException(@"The sequence is empty.", paramName ?? nameof(source));
}
do
{
yield return e.Current;
}
while (e.MoveNext());
}
}
var first = source.ThrowOnNullOrEmpty().First();
当然,您仍然可以在不继续调用链的情况下调用两者。此外,我还包含了paramName,以便调用者可以为错误包含一个替代名称,如果它不是“源”被检查,例如。“nameof(目标)”。