c# – 通过获取原始实体Linq到Collections Group

c# – 通过获取原始实体Linq到Collections Group,第1张

概述我正在使用以下代码对工具集合进行分组: var filteredTools = from t in tools group t by new { t.ModuleName,t.Number} into g sele 我正在使用以下代码对工具集合进行分组:

var filteredTools = from t in tools                               group t by new { t.Modulename,t.Number}                               into g                               select new { Modulename = g.Key,Values = g };

tools是一个简单的集合,定义如下:

List<Tool> tools

执行分组后,我得到3行(从40行),因此分组工作正常.行具有g.Key的键,值是分组条件.无论如何都要将它与原始工具联系起来.也许每个工具的密钥都应该是唯一的,因此在执行分组后,我可以从工具集中获取原始工具.

解决方法 是的,这些工具仍然存在于每个组中:

foreach (var group in filteredTools) {    // This is actually an anonymous type...    Console.Writeline("Module name: {0}",group.Modulename);    foreach (Tool tool in group.Values)    {        Console.Writeline("  Tool: {0}",tool);    }}

说实话,你不需要在这里选择你的匿名类型.你可以使用:

var filteredTools = tools.GroupBy(t => new { t.Modulename,t.Number});foreach (var group in filteredTools) {    // This is actually an anonymous type...    Console.Writeline("Module name: {0}",group.Key);    foreach (Tool tool in group)    {        Console.Writeline("  Tool: {0}",tool);    }}
总结

以上是内存溢出为你收集整理的c# – 通过获取原始实体Linq到Collections Group全部内容,希望文章能够帮你解决c# – 通过获取原始实体Linq到Collections Group所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1217636.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存