C# 通过取回原始实体将Linq发送到集合组

C# 通过取回原始实体将Linq发送到集合组,c#,linq-to-entities,C#,Linq To Entities,我正在使用以下代码对工具集合进行分组: var filteredTools = from t in tools group t by new { t.ModuleName,t.Number} into g select new { ModuleName = g.Key, Values = g }; 工具是一个

我正在使用以下代码对工具集合进行分组:

 var filteredTools = from t in tools
                               group t by new { t.ModuleName,t.Number}
                               into g
                               select new { ModuleName = g.Key, Values = g };
工具是一个简单的集合,定义如下:

List<Tool> tools
列出工具

执行分组后,我得到了3行(从40行返回),所以分组工作正常。行的键为g。键和值是分组条件。是否有任何方法将其与原始工具联系起来。可能每个工具的键都应该是唯一的,因此在执行分组后,我可以从工具集合中获取原始工具

是的,每个组中仍然存在工具:

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);
    }
}
老实说,您不需要在这里为select输入匿名类型。您可以使用:

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);
    }
}