C# - Aggregate( ) 中的终止

发布于 2024-08-12 21:56:29 字数 373 浏览 6 评论 0原文

从下面的模拟中,

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;
 }
);

我想在余额小于提款时终止聚合。但是我的代码遍历了整个数组。如何终止它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

就像说晚安 2024-08-19 21:56:29

在我看来,您需要一个 Accumulate 方法来生成新的累积值序列,而不是标量。像这样的东西:

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;
    }
}

然后你可以应用 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);

我可以发誓在正常的 LINQ to Objects 中存在类似的东西,但我现在找不到它......

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...

番薯 2024-08-19 21:56:29

您应该照常使用Aggregate,然后使用Where 忽略负余额。

顺便说一句,在 LINQ 方法中使用具有副作用的函数(例如 Console.WriteLine)是一种不好的做法。您最好首先执行所有 LINQ 聚合和过滤,然后编写 foreach 循环以打印到控制台。

You should use Aggregate as normal, then use Where to omit negative balances.

BTW, using functions with side effects (such as Console.WriteLine) inside a LINQ method is bad practice. You're better off doing all of the LINQ aggregation and filtering first, then writing a foreach loop to print to the console.

决绝 2024-08-19 21:56:29

用 for 循环替换聚合。

Replace aggregate with for loop.

甜尕妞 2024-08-19 21:56:29

您可能想要使用 TakeWhile().Aggregate() 并检查 take while 谓词中的余额。

You probably want to use TakeWhile().Aggregate() and check the balance in the take while predicate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文