Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8505781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:25:02+00:00 2026-06-11T02:25:02+00:00

I have the following code int someCount = 0; for ( int i =0

  • 0

I have the following code

int someCount = 0;

for ( int i =0 ; i < intarr.Length;i++ )
{
   if ( intarr[i] % 2 == 0 )
   { 
       someCount++;
       continue;
   }
   // Some other logic for those not satisfying the condition
}

Is it possible to use any of the Array.Where or Array.SkiplWhile to achieve the same?

foreach(int i in intarr.where(<<condtion>> + increment for failures) )
{
      // Some other logic for those not satisfying the condition    
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:25:04+00:00Added an answer on June 11, 2026 at 2:25 am

    Nothing (much) prevents you from rolling your own Where that counts the failures. “Nothing much” because neither lambdas nor methods with yield return statements are allowed to reference out/ref parameters, so the desired extension with the following signature won’t work:

    // dead-end/bad signature, do not attempt
    IEnumerable<T> Where(
        this IEnumerable<T> self,
        Func<T,bool> predicate,
        out int failures)
    

    However, we can declare a local variable for the failure-count and return a Func<int> that can get the failure-count, and a local variable is completely valid to reference from lambdas. Thus, here’s a possible (tested) implementation:

    public static class EnumerableExtensions
    {
        public static IEnumerable<T> Where<T>(
            this IEnumerable<T> self,
            Func<T,bool> predicate,
            out Func<int> getFailureCount)
        {
            if (self == null) throw new ArgumentNullException("self");
            if (predicate == null) throw new ArgumentNullException("predicate");
    
            int failures = 0;
    
            getFailureCount = () => failures;
    
            return self.Where(i =>
                {
                    bool res = predicate(i);
                    if (!res)
                    {
                        ++failures;
                    }
                    return res;
                });
        }
    }
    

    …and here’s some test code that exercises it:

    Func<int> getFailureCount;
    int[] items = { 0, 1, 2, 3, 4 };
    foreach(int i in items.Where(i => i % 2 == 0, out getFailureCount))
    {
        Console.WriteLine(i);
    }
    Console.WriteLine("Failures = " + getFailureCount());
    

    The above test, when run outputs:

    0
    2
    4
    Failures = 2

    There are a couple caveats I feel obligated to warn about. Since you could break out of the loop prematurely without having walked the entire IEnumerable<>, the failure-count would only reflect encountered-failures, not the total number of failures as in @nneonneo’s solution (which I prefer.) Also, if the implementation of LINQ’s Where extension were to change in a way that called the predicate more than once per item, then the failure count would be incorrect. One more point of interest is that, from within your loop body you should be able to make calls to the getFailureCount Func to get the current running failure count so-far.

    I presented this solution to show that we are not locked-into the existing prepackaged solutions. The language and framework provides us with lots of opportunities to extend it to suit our needs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: for (int w=0; w<10; w++) { //some lines of
I have following code int i; //gobal var Thread1: { ... i=some value; }
I have the following code: int t = s.length()-1; int g = 0; for
I have the following code: int sec = 62; string str = string.Format(Time: {0:xxx},
I have the following code: int width = 10; int height = 7; bool[,]
I have the following code: int i=1; printf((i==1)? : hello); printf( hello); And I
I have the following code int ParseData(unsigned char *packet, int len) { struct ethhdr
I have the following code int main() { int a=6; void *p; p=&a; p++;
I have the following code: int i = 5000; Console.WriteLine(waiting + i + miliseconds);
I have the following code: int main(int argc, char** argv) { onelog a; std::cout

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.