42

I've been running into situations where I feel I'm lacking a LINQ extension method which effectivelly checks if there is no match of the specified predicate in a collection. There is Any and All, but if I for instance use the following code:

if (Objects.All(u => u.Distance <= 0))

This returns true if all the objects in the collection are 0 or less yards away.

if (Objects.Any(u => u.Distance <= 0))

This returns true if there is at least one object in the collection which is 0 or less yards away from me.

So far so good, both those methods make sense and the syntax for them makes sense too. Now, if I want to check if there is no object with 0 or less distance, I'd have to invert the predicate inside the All method to >= 0 instead of <= 0 or call !All(), which in some cases results in very poorly readable code.

Is there no method which effectively does Collection.None(u => u.Distance <= 0) to check if there is no object in the collection which is 0 or less yards away? It's syntactic sugar more than an actual problem, but I just have the feeling it's missing.

9
  • 3
    Extension methods are you friend here. If you need Collection.None, define one...
    – David Arno
    Oct 12, 2013 at 19:15
  • @DavidArno There is already a method which effectively does what the question asks (Any). The OP doesn't like it, not because it doesn't do what the OP's looking for, but because it doesn't look pretty. What's pretty and what isn't is largely opinion based. I didn't vote, nor am I going to, but I can see why someone else might.
    – user743382
    Oct 12, 2013 at 19:22
  • possible duplicate of Checking if a list is empty with LINQ Oct 12, 2013 at 19:27
  • I'm not asking how to check if a collection is empty, I'm asking if there's a method which returns true if none of the values in the list match the predicate. And I'm not saying I don't like Any, I love Any. I'm asking if there's a method in existence in LINQ which I feel is missing: None.
    – aevitas
    Oct 12, 2013 at 19:50
  • 7
    No, it isn't a duplicate as "Checking if a list is empty with LINQ" is concerned with checking if a list is empty, whereas this question is concerned with handling situations where no elements of a non-empty list match some critia.
    – David Arno
    Oct 12, 2013 at 19:52

3 Answers 3

46

None is the same as !Any, so you could define your own extension method as follows:

public static class EnumerableExtensions
{
    public static bool None<TSource>(this IEnumerable<TSource> source,
                                     Func<TSource, bool> predicate)
    {
        return !source.Any(predicate);
    }
}
7
  • None is the same as !All too, that's why it's so weird that there's no None. I guess I'll stick to writing my own extension as you suggested and hope they'll add it at a later point.
    – aevitas
    Oct 12, 2013 at 19:15
  • 12
    No !Any. !All will succeed from the moment one item doesn't pass the test. None means there is no element that passes the test. !All could be explained as AnyNot. In logic, exist neg x is not equal to neg exists x Oct 12, 2013 at 19:16
  • 6
    @aevitas None is not the same as !All, although one could create a NotAll extension method. But why not HardlyAny or AFew ;) Oct 12, 2013 at 19:16
  • 1
    @aevitas: As you said, using All you have to invert the predicate. Sometimes you need None; that's where I use !Any.
    – dtb
    Oct 12, 2013 at 19:17
  • @dtb None may be helpful as a shorthand but isn't needed. There is no None symbol in predicate logic. It's either NOT ANY or ALL NOT Feb 20, 2017 at 14:21
6

You can write your own Extension Method:

public static bool None(this IEnumerable<T> collection, Func<T, bool> predicate)
{
  return collection.All(p=>predicate(p)==false);
}

Or on IQueryable<T> as well

public static bool None(this IQueryable<T> collection, Expression<Func<TSource, bool>> predicate)
{
  return collection.All(p=> predicate(p)==false);
}
5
  • 3
    using p=>!Predicate(p) is more succinct.
    – King King
    Oct 12, 2013 at 19:19
  • 2
    @KingKing But less readable, although it is my personal idea.
    – Alireza
    Oct 12, 2013 at 19:21
  • @Alireza sure, it's just your feeling, I don't feel so.
    – King King
    Oct 12, 2013 at 19:22
  • 3
    @KingKing Yes.I said it is my own personal feeling nothing general. But sometimes '!' hides itself behind characters :)
    – Alireza
    Oct 12, 2013 at 19:26
  • 2
    == false is less readable, because it's unusual. Also, you can't do predicate(p) in the second case, though you could return !collection.Any(predicate) which would also have the benefit in the first example of not creating a new delegate.
    – Jon Hanna
    Feb 22, 2017 at 3:33
1

Even shorter version

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.