嗨,我有这样的课程..
class man
{
public string name { get; set; }
public string houseid { get; set; }
}
我有一份像这样的男人名单..
List<man> ppl = new List<man>();
我想搜索是否有多个人拥有相同的家庭主妇。如果有不止一个人拥有相同的房屋ID,并且如果拥有相同房屋ID的人数不超过限制5我想要房屋ID和房屋ID的出现次数?如果有少于5个成员的房子,我想要那个房屋ID和那个房子下面的男人数量?怎么做?
答案 0 :(得分:1)
您可以使用:
var houses = ppl.GroupBy(x => x.houseid) // 1
.Where(x => x.Count() < 5) // 2
.Select(x => new { HouseID = x.Key, Population = x.Count() }); // 3
答案 1 :(得分:0)
听起来你需要GroupBy
var houses = ppl.GroupBy(x => x.houseid)
.Where(g => g.Count() < 5)
.Select(g => new { Id = g.Key, Count = g.Count());
foreach (var house in houses)
{
Console.WriteLine("House {0} has a population of {1}", house.Id, house.Count);
}