本文介绍了如何在对象列表中搜索匹配项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
根据 houseid 对人民进行分组
获取少于五个项目的组
为每个包含房屋编号和人口编号的组创建匿名类型.
Group the peoples based on houseid
Get the groups that contains less than five items
Create anonymous type for each group that contains the id of the House and the population.
我有一个像这样的课程.
Hi i have a class like this..
class man { public string name { get; set; } public string houseid { get; set; } }我有一个这样的人名单.
and I have a list of man like this..
List<man> ppl = new List<man>();我想搜索是否有多个具有相同房屋编号的人.如果有一个以上具有相同房屋ID的人,并且具有相同房屋ID的人数不超过限制5,我想要该房屋ID和该房屋ID的出现次数吗?简而言之,如果有少于5名成员的房屋,我需要该房屋ID和该房屋ID下的人数?怎么做?
I want to search if there are more than one person with the same houseid. If there are more than one man with same house id and if the number of people having same house id does not exceed limit 5 I want that house ids and the number of occurrences of that house id? Simply if there are houses with less than 5 members I want that house ids and the number of men under that house id? How to do that?
推荐答案您可以使用:
var houses = ppl.GroupBy(x => x.houseid) // 1 .Where(x => x.Count() < 5) // 2 .Select(x => new { HouseID = x.Key, Population = x.Count() }); // 3
更多推荐
如何在对象列表中搜索匹配项?
发布评论