我喜欢绳子。IsNullOrEmpty方法。我想有一些东西,将允许相同的功能IEnumerable。有这样的东西吗?也许是一些收集助手类?我问的原因是,在if语句中,如果模式是(mylist != null && mylist. any()),代码看起来很混乱。使用Foo.IsAny(myList)会干净得多。
这篇文章没有给出答案:IEnumerable是空的?
我喜欢绳子。IsNullOrEmpty方法。我想有一些东西,将允许相同的功能IEnumerable。有这样的东西吗?也许是一些收集助手类?我问的原因是,在if语句中,如果模式是(mylist != null && mylist. any()),代码看起来很混乱。使用Foo.IsAny(myList)会干净得多。
这篇文章没有给出答案:IEnumerable是空的?
当前回答
另一种方法是获取枚举器并调用MoveNext()方法来查看是否有任何项:
if (mylist != null && mylist.GetEnumerator().MoveNext())
{
// The list is not null or empty
}
这适用于IEnumerable和IEnumerable<T>。
其他回答
这可能会有所帮助
public static bool IsAny<T>(this IEnumerable<T> enumerable)
{
return enumerable?.Any() == true;
}
public static bool IsNullOrEmpty<T>(this IEnumerable<T> enumerable)
{
return enumerable?.Any() != true;
}
下面是@Matt Greer的有用答案的修改版本,它包括一个静态包装器类,这样你就可以将它复制粘贴到一个新的源文件中,不依赖于Linq,并添加了一个泛型IEnumerable<T>重载,以避免非泛型版本会发生的值类型装箱。[编辑:注意,使用IEnumerable<T>并不能阻止对枚举器进行装箱,鸭子类型不能阻止这一点,但至少值类型集合中的元素不会每个都被装箱。]
using System.Collections;
using System.Collections.Generic;
public static class IsNullOrEmptyExtension
{
public static bool IsNullOrEmpty(this IEnumerable source)
{
if (source != null)
{
foreach (object obj in source)
{
return false;
}
}
return true;
}
public static bool IsNullOrEmpty<T>(this IEnumerable<T> source)
{
if (source != null)
{
foreach (T obj in source)
{
return false;
}
}
return true;
}
}
我使用Bool IsCollectionNullOrEmpty = !(Collection?. any ()??false);。希望这能有所帮助。
分解:
. any()如果Collection为空将返回null,如果Collection为空则返回false。
收集? .Any () ?如果Collection为空,则返回false;如果Collection为空,则返回false。
它的补将得到IsEmptyOrNull。
Jon Skeet的回答(https://stackoverflow.com/a/28904021/8207463)有一个很好的方法,使用扩展方法- Any()为NULL和EMPTY。但是他正在验证问题的所有者,以防NOT NULL。 因此,请仔细更改Jon验证AS NULL的方法为:
If (yourList?.Any() != true)
{
..your code...
}
不使用(将不验证为NULL):
If (yourList?.Any() == false)
{
..your code...
}
你也可以在验证AS NOT NULL的情况下(没有测试,只是作为例子,但没有编译器错误)做一些类似使用predicate的事情:
If (yourList?.Any(p => p.anyItem == null) == true)
{
..your code...
}
https://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,8788153112b7ffd0
你可以使用哪个.NET版本,请检查:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.any?view=netframework-4.8#moniker-applies-to
我根据@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;
}
}
扩展方法的命名可能会更好。