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

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


当前回答

当然你可以这样写:

public static class Utils {
    public static bool IsAny<T>(this IEnumerable<T> data) {
        return data != null && data.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

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


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

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

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

我使用

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

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

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

下面是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(项目)实际上将开始在序列中进行步进。(当然,如果你使用了很多序列,你很快就会学会思考通过它们的步骤。)

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