Any T的相反方法是什么? [英] What is the opposite method of Any<T>

查看:79
本文介绍了Any T的相反方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何与Linq一起检查集合中是否不包含对象. IE.与Any<T>相反.

How can I check with Linq if a collection does not contain an object. I.E. The opposite of Any<T>.

我可以用!反转结果,但是出于可读性考虑,我想知道是否还有更好的方法吗?我应该自己添加扩展名吗?

I could invert the result with a ! but for readability I wondered if there was a more better way to do this? Should I add the extension myself?

推荐答案

您可以轻松创建None扩展方法:

You can easily create a None extension method:

public static bool None<TSource>(this IEnumerable<TSource> source)
{
    return !source.Any();
}

public static bool None<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
    return !source.Any(predicate);
}

这篇关于Any T的相反方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆