当前位置: 首页 > news >正文

C#对集合进行分组IGroupingout TKey, out TElement>

C#Linq中可以使用分组方法GroupBy()对集合进行分组,返回一个分组集合对象IEnumerable<IGrouping<TKey, TSource>>

GroupBy()扩展方法

        //// 摘要://     根据指定的键选择器函数对序列的元素进行分组。//// 参数://   source://     System.Collections.Generic.IEnumerable`1 其元素进行分组。////   keySelector://     用于提取每个元素的键的函数。//// 类型参数://   TSource://     中的元素的类型 source。////   TKey://     返回的键的类型 keySelector。//// 返回结果://     IEnumerable<IGrouping<TKey, TSource>> 在 C# 或 IEnumerable(Of IGrouping(Of TKey,//     TSource)) 中 Visual Basic 其中每个 System.Linq.IGrouping`2 对象包含一系列对象和一个密钥。//// 异常://   T:System.ArgumentNullException://     source 或 keySelector 为 null。public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);

分组接口IGrouping<out TKey, out TElement>

using System.Collections;
using System.Collections.Generic;namespace System.Linq
{//// 摘要://     表示具有公共键的对象的集合。//// 类型参数://   TKey://     键的类型 System.Linq.IGrouping`2。////   TElement://     中的值的类型 System.Linq.IGrouping`2。public interface IGrouping<out TKey, out TElement> : IEnumerable<TElement>, IEnumerable{//// 摘要://     获取的键 System.Linq.IGrouping`2。//// 返回结果://     键 System.Linq.IGrouping`2。TKey Key { get; }}
}

示例程序:

控制台应用程序,这里自定义类CustomType使用元组来替代

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace GroupingLinqDemo
{class Program{static void Main(string[] args){List<Tuple<int, int, string>> tuples = new List<Tuple<int, int, string>>();tuples.Add(Tuple.Create(0, 0, "百里屠苏"));tuples.Add(Tuple.Create(0, 1, "风晴雪"));tuples.Add(Tuple.Create(0, 2, "方兰生"));tuples.Add(Tuple.Create(1, 0, "乐无异"));tuples.Add(Tuple.Create(1, 1, "闻人羽"));tuples.Add(Tuple.Create(2, 0, "北洛"));tuples.Add(Tuple.Create(2, 1, "云无月"));tuples.Add(Tuple.Create(2, 2, "岑缨"));tuples.Add(Tuple.Create(2, 3, "巫炤"));List<IGrouping<int, Tuple<int, int, string>>> groupingList = tuples.GroupBy(x => x.Item1).ToList();for (int i = 0; i < groupingList.Count; i++){List<string> dataList = new List<string>();foreach (Tuple<int, int, string> item in groupingList[i]){dataList.Add(item.ToString());}Console.WriteLine($"古剑奇谭{groupingList[i].Key + 1}:");Console.WriteLine($"\x20\x20\x20\x20【{string.Join(";", dataList)}】");}Console.ReadLine();}}
}

运行如图:


文章转载自:

http://OF2JR1vT.qhkdt.cn
http://61M9iZtk.qhkdt.cn
http://4L0trc3U.qhkdt.cn
http://tUhD8b5y.qhkdt.cn
http://k6pFhWke.qhkdt.cn
http://OIDqWIRd.qhkdt.cn
http://1yOm9NlW.qhkdt.cn
http://PYga5k1c.qhkdt.cn
http://G0hiqZiB.qhkdt.cn
http://j7KHlUJQ.qhkdt.cn
http://FR9p93uz.qhkdt.cn
http://OH6B90Mw.qhkdt.cn
http://SWOuMRsp.qhkdt.cn
http://VeiQJWlO.qhkdt.cn
http://1d6STqQ7.qhkdt.cn
http://oMi8LXQ5.qhkdt.cn
http://7LO67TEp.qhkdt.cn
http://1MEFRZ3d.qhkdt.cn
http://3aqY1ZzU.qhkdt.cn
http://VrHF5RGi.qhkdt.cn
http://5d9IQQGc.qhkdt.cn
http://nt8TKjcm.qhkdt.cn
http://OzoDYOhE.qhkdt.cn
http://x6SDwVuV.qhkdt.cn
http://LlKzo4ZZ.qhkdt.cn
http://NHSslfj2.qhkdt.cn
http://1PBrhFT3.qhkdt.cn
http://CM5CrF2p.qhkdt.cn
http://8JolOlEH.qhkdt.cn
http://xRAMd22v.qhkdt.cn
http://www.dtcms.com/a/206464.html

相关文章:

  • day19-20-四剑客-find-grep-sed-awk
  • C# 大文件分割
  • TensorFlow简介与使用指南
  • 学习笔记:黑马程序员JavaWeb开发教程(2025.4.11)
  • 计算机网络 第三章:运输层(三)
  • 解决自签名证书HTTPS告警:强制使用SHA-256算法生成证书
  • 微软CTO:AI已经“能力过剩”,行业需要努力缩小模型能力与实际产品交付之间的差距
  • AUTOSAR AP 入门0:AUTOSAR_EXP_PlatformDesign.pdf
  • ACM知识点总结 -【搜索技术】
  • 【机器学习】欠拟合、过拟合和正则化
  • 高性能图表库SciChart WPF v8.8全新发布——提升渐变颜色映射高度
  • 力扣-长度最小的子数组
  • Linux操作系统:fork+exec进程创建
  • 如果教材这样讲--碳膜电阻、金属氧化膜电阻、金属膜电阻、保险丝电阻、绕线电阻的区别和用途
  • Docker 与微服务架构:从单体应用到容器化微服务的迁移实践
  • 微软全新开源的Agentic Web网络项目:NLWeb,到底是什么 ?
  • 2024-2025年AI领域重大事件深度解析:技术革命、产业重构与未来挑战
  • 机器学习与深度学习算法:从决策树到 GAN 的原理与 PyTorch 实现
  • 升级node@22后运行npm install报错 distutils not found
  • leetcode 83和84 Remove Duplicates from Sorted List 和leetcode 1836
  • 微服务的应用案例
  • JMeter-Websocket接口自动化
  • C++23中std::span和std::basic_string_view可平凡复制提案解析
  • SpringBoot的前世今生
  • 学习STC51单片机11(芯片为STC89C52RC)
  • 使用VLOOKUP查询两个表格,使用数字格式进行查询,如果返回NA错误,则使用文本格式进行查询
  • 实战:Dify智能体+Java=自动化运营工具!
  • 1 研发规划
  • java基础(方法)
  • Spring Boot——自动配置