集团使用LINQ变量整数范围 [英] Group by variable integer range using Linq
问题描述
我试图将一组基于一个整数范围内的数据,由范围在一个固定的时间间隔不会增加。
I'm trying to group a set of data based on the range of an integer, by the range does not increase at a fixed interval.
例如。我有
商品编号价格
1         10
2         30
3         50
4         120
Item ID Price
1 10
2 30
3 50
4 120
我想组价格0的物品 - 10,11 - 100和100-500。因此,该项目1在A组,项目2,3,B组,第4项在C组。
I would like to group the items with price 0 - 10, 11- 100, and 100-500. So that item 1 is in group A, item 2,3, in group B, item 4 in group C.
我可以想出最接近的是
从项目
按组项目(items.price / 10)
The closest I can come up is from items group items by (items.price / 10 )
再加入团体合力得到不同的范围。
then join the groups together to get the different ranges.
任何想法?
谢谢!
珍妮
推荐答案
参数设置范围上限的列表...
Parameterizing the list of range ceilings...
var ceilings = new[] { 10, 100, 500 };
var groupings = items.GroupBy(item => ceilings.First(ceiling => ceiling >= item));
这篇关于集团使用LINQ变量整数范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!