如何在对象列表中搜索匹配项? [英] how to search for matching items in an object list?

查看:79
本文介绍了如何在对象列表中搜索匹配项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的课程.

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

  1. 根据 houseid
  2. 对人民进行分组
  3. 获取少于五个项目的组
  4. 为每个包含房屋编号和人口编号的组创建匿名类型.
  1. Group the peoples based on houseid
  2. Get the groups that contains less than five items
  3. Create anonymous type for each group that contains the id of the House and the population.

这篇关于如何在对象列表中搜索匹配项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆