Linq TakeWhile 取决于元素的总和(或聚合)
我有一个元素列表,想要取总和(或元素的任何聚合)满足某个条件。下面的代码完成了这项工作,但我很确定这不是一个不寻常的问题,应该存在适当的模式。
var list = new List<int> { 1, 2, 3, 4, 5, 6, 7 };
int tmp = 0;
var listWithSum = from x in list
let sum = tmp+=x
select new {x, sum};
int MAX = 10;
var result = from x in listWithSum
where x.sum < MAX
select x.x;
有人知道如何以更好的方式解决任务,可能将 TakeWhile 和 Aggregate 合并到一个查询中?
谢谢
I have a list of elements and want to takeWhile the sum (or any aggregation of the elements) satisfy a certain condition. The following code does the job, but i am pretty sure this is not an unusual problem for which a proper pattern should exist.
var list = new List<int> { 1, 2, 3, 4, 5, 6, 7 };
int tmp = 0;
var listWithSum = from x in list
let sum = tmp+=x
select new {x, sum};
int MAX = 10;
var result = from x in listWithSum
where x.sum < MAX
select x.x;
Does somebody know how to solve the task in nicer way, probably combining TakeWhile and Aggregate into one query?
Thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,您想要类似 Reactive Extensions(System.Interactive 部分) - 它类似于
Aggregate
,但产生一个序列而不是单个结果。然后你可以这样做:(MoreLINQ 有一个 类似运算符,顺便说一句 - 但目前它不支持累加器的想法输入序列不是同一类型。)
It seems to me that you want something like the
Scan
method from Reactive Extensions (the System.Interactive part) - it's likeAggregate
, but yields a sequence instead of a single result. You could then do:(MoreLINQ has a similar operator, btw - but currently it doesn't support the idea of the accumulator and input sequence not being the same type.)
我最近解决了一个类似的任务,并决定创建一个单独的扩展方法以避免第三方库:
用法:
输出: 1, 2, 3
扩展方法:
I recently solved a similar task and decided to create a separate extension method to avoid third-party libraries:
Usage:
Output: 1, 2, 3
Extension method: