按用户的年龄分组用户 [英] Group users by age for a range
问题描述
我有一些数据,我需要做一些统计。我需要按年龄分组用户。
var byAge = displayResult.GroupBy(x => x.Age);
我可以像上面那样做。然而,这给了我19,20岁,21岁等我想要的是年龄10岁组,如10至20岁,20岁以上的用户
30岁,30-40岁等。
我怎样才能得到这个?
<$ c $ c> var byAge = displayResult.GroupBy(x => 10 *(x.Age / 10));
所有介于0和10之间的数字都将在存储区0中。
从10到20将在 20
之下的关键字 10
,从20到30之间,等等开。
I have some data which i need to make some statistics. I need to group the users by age.
var byAge = displayResult.GroupBy(x => x.Age);
Which i can do as above. However, this gives me ages like 19, 20, 21 etc. what I want is grouping age by 10 years, such as
users between 10-20 yearsold, 20-30 years old, 30-40 years old etc.
How can i get that?
You can truncate the trailing digit by dividing by ten using integer division, and then multiplying back by ten.
var byAge = displayResult.GroupBy(x => 10*(x.Age/10));
Everyone between 0, inclusive, and 10, exclusive, will be in the bucket 0.
from 10 to 20 will be under the key 10
, from 20 to 30 - under the key 20
, and so on.
这篇关于按用户的年龄分组用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!