Group users by age for a range

六月ゝ 毕业季﹏ 提交于 2019-12-13 14:03:11

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/11279245/group-users-by-age-for-a-range

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!