如何在对象列表中搜索匹配项?

编程入门 行业动态 更新时间:2024-10-19 03:25:19
本文介绍了如何在对象列表中搜索匹配项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个像这样的课程.

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

  • 根据 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.
  • 更多推荐

    如何在对象列表中搜索匹配项?

    本文发布于:2023-11-07 06:04:42,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1565733.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:对象   如何在   列表中

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!