Is there a better way of calling LINQ Any + NOT All?

后端 未结 8 945
走了就别回头了
走了就别回头了 2021-02-18 14:46

I need to check if a sequence has any items satisfying some condition but at the same time NOT all items satisfying the same condition.

For example, for a sequence of 10

相关标签:
8条回答
  • 2021-02-18 15:14

    You could define your own extension method. This version is more verbose, but still readable, and it only enumerates the IEnumerable once:

    bool AnyButNotAll<ItemT>(this IEnumerable<ItemT> sequence, Func<ItemT, bool> predicate)
    {
        bool seenTrue = false;
        bool seenFalse = false;
    
        foreach (ItemT item in sequence)
        {
            bool predResult = predicate(item);
            if (predResult)
                seenTrue = true;
            if (!predResult)
                seenFalse = true;
    
            if (seenTrue && seenFalse)
                return true;
        }
    
        return false;
    }
    

    Much shorter, but enumerates the IEnumerable twice:

    bool AnyButNotAll<ItemT>(this IEnumerable<ItemT> sequence, Func<ItemT, bool> predicate)
    {
        return sequence.Any(predicate) && !sequence.All(predicate);
    }
    
    0 讨论(0)
  • 2021-02-18 15:14

    You can use the Aggregate method to do both things at once. I would suggest to use an anonymous type for the TAccumulate, containing both counters. After the aggregation you can read both values from the resulting anonymous type.

    (I can't type an example, I'm on my phone)

    See the documentation here: https://msdn.microsoft.com/en-us/library/vstudio/bb549218(v=vs.100).aspx

    0 讨论(0)
  • 2021-02-18 15:16

    If you wanted to define this as a method, you could take then approach Linq takes in defining both IEnumerable<T> and IQueryable<T> extension methods. This allows for the optimal approach to be taken automatically:

    public static bool SomeButNotAll<T>(this IQueryable<T> source, Expression<Func<T, bool>> predicate)
    {
      if(source == null)
        throw new ArgumentNullException("source");
      if(predicate == null)
        throw new ArgumentNullException("predicate");
      return source.
        Select(predicate)
        .Distinct()
        .Take(2)
        .Count() == 2;
    }
    public static bool SomeButNotAll<T>(this IEnumerable<T> source, Func<T, bool> predicate)
    {
      if(source == null)
        throw new ArgumentNullException("source");
      if(predicate == null)
        throw new ArgumentNullException("predicate");
      using(var en = source.GetEnumerator())
        if(en.MoveNext())
        {
          bool first = predicate(en.Current);
          while(en.MoveNext())
            if(predicate(en.Current) != first)
              return true;
        }
      return false;
    }
    

    If you're using EntityFramework (or another provider that provides a CountAsync you can also provide an asynchronous version easily:

    public static async Task<bool> SomeButNotAllAsync<T>(this IQueryable<T> source, Expression<Func<T, bool>> predicate, CancellationToken cancel)
    {
      if(source == null)
        throw new ArgumentNullException("source");
      if(predicate == null)
        throw new ArgumentNullException("predicate");
      cancel.ThrowIfCancellationRequested();
      return await source.
        Select(predicate)
        .Distinct()
        .Take(2)
        .CountAsync(cancel)
        .ConfigureAwait(false) == 2;
    }
    public static Task<bool> SomeButNotAllAsync<T>(this IQueryable<T> source, Expression<Func<T, bool>> predicate)
    {
      return source.SomeButNotAllAsync(predicate, CancellationToken.None);
    }
    
    0 讨论(0)
  • 2021-02-18 15:18

    You'll like this.

    var anyButNotAll = mySequence
        .Select(item => item.SomeStatus == SomeConst)
        .Distinct()
        .Take(2)
        .Count() == 2;
    

    The Take(2) stops it iterating over any more elements than it has to.

    0 讨论(0)
  • 2021-02-18 15:24

    You could put your predicate in a variable so that you don't have to repeat the predicate twice:

    Func<MyItemType, bool> myPredicate = item => item.SomeStatus == SomeConst;
    if (mySequence.Any(myPredicate) && !mySequence.All(myPredicate))
        ...
    
    0 讨论(0)
  • 2021-02-18 15:26

    If your concern is iterating through all of the elements in a large collection, you're okay - Any and All will short-circuit as soon as possible.

    The statement

    mySequence.Any (item => item.SomeStatus == SomeConst)
    

    will return true as soon as one element that meets the condition is found, and

    !mySequence.All (item => item.SomeStatus == SomeConst)
    

    will return true as soon as one element does not.

    Since both conditions are mutually exclusive, one of the statements is guaranteed to return after the first element, and the other is guaranteed to return as soon as the first element is found.


    As pointed out by others, this solution requires starting to iterate through the collection twice. If obtaining the collection is expensive (such as in database access) or iterating over the collection does not produce the same result every time, this is not a suitable solution.

    0 讨论(0)
提交回复
热议问题