如何使用 Linq 对每 N 行进行分组 [英] How to use Linq to group every N number of rows

查看:24
本文介绍了如何使用 Linq 对每 N 行进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到使这项工作成功的方法,希望有人有想法.一个简化的例子是有一个整数 1-100 的列表,我想每 3 行分组,所以结果将是第一组中的 1,2,3 然后下一组中的 4,5,6 等等.我知道如何获取每第 n 条记录,但我需要的是所有记录,以便我可以使用 first、last、sum、max 等聚合它们.

I cannot find a way to make this work and hoping someone has an idea. A simplified example would be having a list of say integers 1-100, i want to group every 3 rows so the result would be 1,2,3 in first group then 4,5,6 in next group etc. I know how to get every nth record but what I need is all the records so I can then aggregate them, using first, last, sum, max, etc.

谢谢!

推荐答案

var q = Enumerable.Range(0, 100).GroupBy(x => x/3);

我错过了什么吗?

这篇关于如何使用 Linq 对每 N 行进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆