我喜欢绳子。IsNullOrEmpty方法。我想有一些东西,将允许相同的功能IEnumerable。有这样的东西吗?也许是一些收集助手类?我问的原因是,在if语句中,如果模式是(mylist != null && mylist. any()),代码看起来很混乱。使用Foo.IsAny(myList)会干净得多。

这篇文章没有给出答案:IEnumerable是空的?


public static bool IsNullOrEmpty<T>(this IEnumerable<T> enumerable) {
    return enumerable == null || !enumerable.Any();
}

当然你可以这样写:

public static class Utils {
    public static bool IsAny<T>(this IEnumerable<T> data) {
        return data != null && data.Any();
    }
}

但是,要注意不是所有序列都是可重复的;一般来说,我宁愿只遛一次,以防万一。


下面是Marc Gravell回答的代码,以及使用它的示例。

using System;
using System.Collections.Generic;
using System.Linq;

public static class Utils
{
    public static bool IsAny<T>(this IEnumerable<T> data)
    {
        return data != null && data.Any();
    }
}

class Program
{
    static void Main(string[] args)
    {
        IEnumerable<string> items;
        //items = null;
        //items = new String[0];
        items = new String[] { "foo", "bar", "baz" };

        /*** Example Starts Here ***/
        if (items.IsAny())
        {
            foreach (var item in items)
            {
                Console.WriteLine(item);
            }
        }
        else
        {
            Console.WriteLine("No items.");
        }
    }
}

正如他所说,并不是所有序列都是可重复的,因此代码有时可能会引起问题,因为IsAny()开始遍历序列。我怀疑Robert Harvey的回答的意思是,您通常不需要检查null和empty。通常,您可以只检查null,然后使用foreach。

为了避免两次启动序列并利用foreach,我只写了一些像这样的代码:

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        IEnumerable<string> items;
        //items = null;
        //items = new String[0];
        items = new String[] { "foo", "bar", "baz" };

        /*** Example Starts Here ***/
        bool isEmpty = true;
        if (items != null)
        {
            foreach (var item in items)
            {
                isEmpty = false;
                Console.WriteLine(item);
            }
        }
        if (isEmpty)
        {
            Console.WriteLine("No items.");
        }
    }
}

我猜扩展方法为您节省了几行输入,但这段代码对我来说似乎更清楚。我怀疑一些开发人员不会立即意识到IsAny(项目)实际上将开始在序列中进行步进。(当然,如果你使用了很多序列,你很快就会学会思考通过它们的步骤。)


下面是@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;
    }
}

另一种方法是获取枚举器并调用MoveNext()方法来查看是否有任何项:

if (mylist != null && mylist.GetEnumerator().MoveNext())
{
    // The list is not null or empty
}

这适用于IEnumerable和IEnumerable<T>。


只需添加使用系统。Linq和看到魔术发生时,你试图访问可用的方法在IEnumerable。添加它将使您能够访问名为Count()的方法,就像这样简单。只要记住在调用count():)之前检查空值即可。


我的做法是,利用一些现代c#特性:

选择1)

public static class Utils {
    public static bool IsNullOrEmpty<T>(this IEnumerable<T> list) {
        return !(list?.Any() ?? false);
    }
}

选择2)

public static class Utils {
    public static bool IsNullOrEmpty<T>(this IEnumerable<T> list) {
        return !(list?.Any()).GetValueOrDefault();
    }
}

顺便说一下,永远不要使用Count == 0或Count() == 0来检查一个集合是否为空。总是使用Linq的.Any()


我用简单的if来检查它

看看我的解决方案

foreach (Pet pet in v.Pets)
{
    if (pet == null)
    {
        Console.WriteLine(" No pet");// enumerator is empty
        break;
    }
    Console.WriteLine("  {0}", pet.Name);
}

从c# 6开始,你可以使用空传播:myList?.Any() == true

如果你仍然觉得这个方法太麻烦或者更喜欢一个好的扩展方法,我会推荐Matt Greer和Marc Gravell的答案,但为了完整性,还需要一些扩展功能。

他们的答案提供了相同的基本功能,但各从不同的角度。Matt的答案使用了字符串。isnullorempty心态,而Marc的答案采用了Linq的. any()方法来完成工作。

我个人倾向于使用.Any()方法,但希望从该方法的其他重载中添加条件检查功能:

    public static bool AnyNotNull<T>(this IEnumerable<T> source, Func<T, bool> predicate = null)
    {
        if (source == null) return false;
        return predicate == null
            ? source.Any()
            : source.Any(predicate);
    }

所以你仍然可以做这样的事情: myList.AnyNotNull(项= >项目。AnswerToLife == 42);就像使用常规的.Any()一样,但添加了空检查

注意,使用c# 6的方式:myList?.Any()返回bool?而不是bool类型,这是传播null的实际效果


我根据@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;
    }
}

扩展方法的命名可能会更好。


这可能会有所帮助

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;
}

我使用Bool IsCollectionNullOrEmpty = !(Collection?. any ()??false);。希望这能有所帮助。

分解:

. any()如果Collection为空将返回null,如果Collection为空则返回false。

收集? .Any () ?如果Collection为空,则返回false;如果Collection为空,则返回false。

它的补将得到IsEmptyOrNull。


我也遇到过同样的问题,我是这样解决的:

    public bool HasMember(IEnumerable<TEntity> Dataset)
    {
        return Dataset != null && Dataset.Any(c=>c!=null);
    }

“c = > c !=null"将忽略所有null实体。


另一个最佳解决方案如下检查是否为空?

for(var item in listEnumerable)
{
 var count=item.Length;
  if(count>0)
  {
         // not empty or null
   }
  else
  {
       // empty
  }
}

if (collection?.Any() == true){
    // if collection contains more than one item
}
if (collection?.Any() != true){
    // if collection is null
    // if collection does not contain any item
}

我用的是这个:

    public static bool IsNotEmpty(this ICollection elements)
    {
        return elements != null && elements.Count > 0;
    }

埃杰姆:

List<string> Things = null;
if (Things.IsNotEmpty())
{
    //replaces ->  if (Things != null && Things.Count > 0) 
}

由于一些资源在一次读取后会耗尽,我想为什么不将检查和读取结合起来,而不是传统的单独检查,然后读取。

首先,我们有一个更简单的检查为空的内联扩展:

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(目标)”。


 public static bool AnyNotNull<TSource>(this IEnumerable<TSource> source)
    {
        return source != null && source.Any();
    }

我自己的扩展方法来检查非空和任何


没有自定义助手,我也推荐?.Any() ??. any () == true相对简洁,只需要指定一次序列。


当我想要像对待空集合一样对待一个缺失的集合时,我使用以下扩展方法:

public static IEnumerable<T> OrEmpty<T>(this IEnumerable<T> sequence)
{
    return sequence ?? Enumerable.Empty<T>();
}

这个函数可以与所有LINQ方法和foreach组合,而不仅仅是. any(),这就是为什么我更喜欢它而不是人们在这里提出的更专业的帮助函数。


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


我使用

    list.Where (r=>r.value == value).DefaultIfEmpty().First()

如果不匹配,结果将为空,否则返回其中一个对象

如果您想要列表,我相信离开First()或调用ToList()将提供列表或null。


当它是关于引用(可空)类型的,并且项中不期望有空项时,可以使用这一行进行验证

myCollection?.FirstOrDefault() == null

它为null将返回true

enter    public static bool IsNullOrEmpty<T>(this IEnumerable<T> enumerable)
    {

        try
        {
            return enumerable?.Any() != true;
        }
        catch (Exception)
        {

            return true;
        }
   
    }

代码