繁体   English   中英

使用LINQ检查列表是否为空

[英]Checking if a list is empty with LINQ

什么是“最佳”(考虑到速度和可读性)的方式来确定列表是否为空? 即使列表的类型为IEnumerable<T>且没有Count属性。

现在我在这之间折腾:

if (myList.Count() == 0) { ... }

还有这个:

if (!myList.Any()) { ... }

我的猜测是第二个选项更快,因为它会在看到第一个项目后立即返回结果,而第二个选项(对于IEnumerable)将需要访问每个项目以返回计数。

话虽如此,第二个选项看起来是否可读? 你更喜欢哪个? 或者你能想出一个更好的方法来测试空列表吗?

编辑 @ lassevk的响应似乎是最合乎逻辑的,再加上一些运行时检查,如果可能的话,使用缓存计数,如下所示:

public static bool IsEmpty<T>(this IEnumerable<T> list)
{
    if (list is ICollection<T>) return ((ICollection<T>)list).Count == 0;

    return !list.Any();
}

你可以这样做:

public static Boolean IsEmpty<T>(this IEnumerable<T> source)
{
    if (source == null)
        return true; // or throw an exception
    return !source.Any();
}

编辑 :请注意,如果底层源实际上具有快速Count属性,那么简单地使用.Count方法将会很快。 上面的有效优化是检测一些基类型并简单地使用它们的.Count属性,而不是.Any()方法,但如果不能保证,则回退到.Any()。

我会对你似乎已经解决的代码做一个小的补充:检查ICollection ,因为这甚至是由一些非过时的泛型类(即Queue<T>Stack<T> )实现的。 我也将使用as代替is因为它是更地道,并已被证明是更快的

public static bool IsEmpty<T>(this IEnumerable<T> list)
{
    if (list == null)
    {
        throw new ArgumentNullException("list");
    }

    var genericCollection = list as ICollection<T>;
    if (genericCollection != null)
    {
        return genericCollection.Count == 0;
    }

    var nonGenericCollection = list as ICollection;
    if (nonGenericCollection != null)
    {
        return nonGenericCollection.Count == 0;
    }

    return !list.Any();
}

LINQ本身必须以某种方式围绕Count()方法进行一些严格的优化。

这让你感到惊讶吗? 我想,对于IList实现, Count只是直接读取元素的数量,而Any必须查询IEnumerable.GetEnumerator方法,创建一个实例并至少MoveNext一次MoveNext

/编辑@Matt:

我只能假设IEnumerable的Count()扩展方法是这样的:

是的,当然可以。 这就是我的意思。 实际上,它使用ICollection而不是IList但结果是一样的。

我刚写了一个快速测试,试试这个:

 IEnumerable<Object> myList = new List<Object>();

 Stopwatch watch = new Stopwatch();

 int x;

 watch.Start();
 for (var i = 0; i <= 1000000; i++)
 {
    if (myList.Count() == 0) x = i; 
 }
 watch.Stop();

 Stopwatch watch2 = new Stopwatch();

 watch2.Start();
 for (var i = 0; i <= 1000000; i++)
 {
     if (!myList.Any()) x = i;
 }
 watch2.Stop();

 Console.WriteLine("myList.Count() = " + watch.ElapsedMilliseconds.ToString());
 Console.WriteLine("myList.Any() = " + watch2.ElapsedMilliseconds.ToString());
 Console.ReadLine();

第二个差不多慢三倍:)

再次使用堆栈或数组或其他场景尝试秒表测试,它实际上取决于它看起来的列表类型 - 因为它们证明Count更慢。

所以我想这取决于你正在使用的列表类型!

(只是要指出,我在列表中放置了2000多个对象,计数仍然更快,与其他类型相反)

根据Microsoft的文档, List.Count是O(1):
http://msdn.microsoft.com/en-us/library/27b47ht3.aspx

所以只需使用List.Count == 0它比查询快得多

这是因为它有一个名为Count的数据成员,只要在列表中添加或删除某些内容就会更新,因此当您调用List.Count它不必遍历每个元素来获取它,它只返回数据会员。

如果您有多个项目,第二个选项会快得多。

  • Any()找到1个项目, Any()返回。
  • Count()必须继续浏览整个列表。

例如,假设枚举有1000个项目。

  • Any()会检查第一个,然后返回true。
  • 遍历整个枚举后, Count()将返回1000。

如果使用其中一个谓词覆盖,这可能会更糟糕 - Count()仍然必须检查每个项目,即使它只有一个匹配项。

你已经习惯了使用Any - 它确实有意义且可读。

一个警告 - 如果你有一个List,而不仅仅是一个IEnumerable,那么使用该列表的Count属性。

@Konrad让我感到惊讶的是,在我的测试中,我将列表传递给接受IEnumerable<T> ,因此运行时无法通过调用IList<T>的Count()扩展方法来优化它。

我只能假设IEnumerable的Count()扩展方法是这样的:

public static int Count<T>(this IEnumerable<T> list)
{
    if (list is IList<T>) return ((IList<T>)list).Count;

    int i = 0;
    foreach (var t in list) i++;
    return i;
}

...换句话说,针对IList<T>的特殊情况进行了一些运行时优化。

/编辑@Konrad +1伙伴 - 你更可能在ICollection<T>

好的,那这个怎么样?

public static bool IsEmpty<T>(this IEnumerable<T> enumerable)
{
    return !enumerable.GetEnumerator().MoveNext();
}

编辑:我刚刚意识到有人已经草拟了这个解决方案。 有人提到Any()方法会这样做,但为什么不自己做呢? 问候

另一个想法:

if(enumerable.FirstOrDefault() != null)

但是我更喜欢Any()方法。

这对于使其与Entity Framework一起使用至关重要:

var genericCollection = list as ICollection<T>;

if (genericCollection != null)
{
   //your code 
}

这是我对Dan Tao的回答的实现,允许谓词:

public static bool IsEmpty<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
    if (source == null) throw new ArgumentNullException();
    if (IsCollectionAndEmpty(source)) return true;
    return !source.Any(predicate);
}

public static bool IsEmpty<TSource>(this IEnumerable<TSource> source)
{
    if (source == null) throw new ArgumentNullException();
    if (IsCollectionAndEmpty(source)) return true;
    return !source.Any();
}

private static bool IsCollectionAndEmpty<TSource>(IEnumerable<TSource> source)
{
    var genericCollection = source as ICollection<TSource>;
    if (genericCollection != null) return genericCollection.Count == 0;
    var nonGenericCollection = source as ICollection;
    if (nonGenericCollection != null) return nonGenericCollection.Count == 0;
    return false;
}

如果我用Count()检查,Linq在数据库中执行“SELECT COUNT(*)..”,但我需要检查结果是否包含数据,我决定引入FirstOrDefault()而不是Count();

之前

var cfop = from tabelaCFOPs in ERPDAOManager.GetTable<TabelaCFOPs>()

if (cfop.Count() > 0)
{
    var itemCfop = cfop.First();
    //....
}

var cfop = from tabelaCFOPs in ERPDAOManager.GetTable<TabelaCFOPs>()

var itemCfop = cfop.FirstOrDefault();

if (itemCfop != null)
{
    //....
}
private bool NullTest<T>(T[] list, string attribute)

    {
        bool status = false;
        if (list != null)
        {
            int flag = 0;
            var property = GetProperty(list.FirstOrDefault(), attribute);
            foreach (T obj in list)
            {
                if (property.GetValue(obj, null) == null)
                    flag++;
            }
            status = flag == 0 ? true : false;
        }
        return status;
    }


public PropertyInfo GetProperty<T>(T obj, string str)

    {
        Expression<Func<T, string, PropertyInfo>> GetProperty = (TypeObj, Column) => TypeObj.GetType().GetProperty(TypeObj
            .GetType().GetProperties().ToList()
            .Find(property => property.Name
            .ToLower() == Column
            .ToLower()).Name.ToString());
        return GetProperty.Compile()(obj, str);
    }
List<T> li = new List<T>();
(li.First().DefaultValue.HasValue) ? string.Format("{0:yyyy/MM/dd}", sender.First().DefaultValue.Value) : string.Empty;

myList.ToList().Count == 0 就这样

这个扩展方法适合我:

public static bool IsEmpty<T>(this IEnumerable<T> enumerable)
{
    try
    {
        enumerable.First();
        return false;
    }
    catch (InvalidOperationException)
    {
        return true;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM