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

  • Home
  • SEARCH
  • 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 517111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:48:02+00:00 2026-05-13T07:48:02+00:00

From the following simulation int[] amountWithdrawal = { 10, 20, 30, 140, 50, 70

  • 0

From the following simulation

int[] amountWithdrawal = { 10, 20, 30, 140, 50, 70 };

amountWithdrawal.Aggregate(100, (balance, withdrawal) => 
{
  Console.WriteLine("balance :{0},Withdrawal:{1}", balance, withdrawal);
 if (balance >= withdrawal)
 {
   return balance - withdrawal;
 }
 else return balance;
 }
);

I want to terminate the Aggregation when the balance is less than the withdrawal.But my code travels the entire array.How to terminate it?

  • 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-05-13T07:48:02+00:00Added an answer on May 13, 2026 at 7:48 am

    It seems to me that you want an Accumulate method which yields a new sequence of accumulated values, instead of a scalar. Something like this:

    public static IEnumerable<TAccumulate> SequenceAggregate<TSource, TAccumulate>(
        this IEnumerable<TSource> source,
        TAccumulate seed,
        Func<TAccumulate, TSource, TAccumulate> func)
    {
        TAccumulate current = seed;
        foreach (TSource item in source)
        {
            current = func(current, item);
            yield return current;
        }
    }
    

    Then you could apply TakeWhile:

    int[] amountWithdrawal = { 10, 20, 30, 140, 50, 70 };
    
    var query = amountWithdrawal.SequenceAggregate(100, (balance, withdrawal) => 
    {
      Console.WriteLine("balance :{0},Withdrawal:{1}", balance, withdrawal);
      return balance - withdrawal;
    }).TakeWhile (balance => balance >= 0);
    

    I could have sworn there was something like this in normal LINQ to Objects, but I can’t find it at the moment…

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

Sidebar

Related Questions

I'm trying to stop the browser from following certain links, rather I want to
I've got the following situation: var totalRecords = (from entry in invalidAccEntryRepository select entry).Where(entry
I have the following situation: There is a tool that gets an XSLT from
I have the following situation. Im getting data from an external system in the
We've got the following situation, running from a single domain: Page A uses window.open()
I have the following situation: I have an application that collects some data from
While going through university and from following the development of SO, I've heard a
Is it possible to prevent the browser from following redirects when sending XMLHttpRequest-s (i.e.
I used DataSet to load a schema from following xml file; <node id=0> <node
I'd like to create a dropdownlist from following structure List<KeyValuePair<long, string>> sponsori = new

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.