Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按键的多个属性对字典进行分组_C#_Linq_Dictionary_Grouping - Fatal编程技术网

C# 按键的多个属性对字典进行分组

C# 按键的多个属性对字典进行分组,c#,linq,dictionary,grouping,C#,Linq,Dictionary,Grouping,我有一个下面的代码,它根据列表元素的三个属性对列表进行分组,并返回列表列表: List<Employee> data; var groups = data.GroupBy(x => new { x.Company, x.Position, x.Age }) .Select(grp => grp.ToList()) .ToList(); 列表数据; var groups=data.GroupBy(x=>n

我有一个下面的代码,它根据列表元素的三个属性对列表进行分组,并返回列表列表:

List<Employee> data;
var groups = data.GroupBy(x => new { x.Company, x.Position, x.Age })
                 .Select(grp => grp.ToList())
                 .ToList();
列表数据;
var groups=data.GroupBy(x=>new{x.Company,x.Position,x.Age})
.Select(grp=>grp.ToList())
.ToList();
无法更改此代码以使用字典:

Dictionary<Employee, Report> data;
字典数据;

我需要创建一个按dictionary key的这三个属性分组的字典列表。

您至少有两个选项:

首先:对字典的键属性使用groupping,然后将字典的值投影到组:

    Dictionary<Employee, Report> data;
    var groups = data
                 .GroupBy(x => new { x.Key.Company, x.Key.Position, x.Key.Age }, pair => pair.Value)
                 .Select(grp => grp.ToList())
                 .ToList();
字典数据;
变量组=数据
.GroupBy(x=>new{x.Key.Company,x.Key.Position,x.Key.Age},pair=>pair.Value)
.Select(grp=>grp.ToList())
.ToList();
Second:正如@Z.R.T.所说,您只能首先投影字典的值,然后投影组:

    Dictionary<Employee, Report> data;
    var groups = data
                 .Select(pair => pair.Key)
                 .GroupBy(x => new { x.Company, x.Position, x.Age }, pair => pair.Value)
                 .Select(grp => grp.ToList())
                 .ToList();
字典数据;
变量组=数据
.Select(pair=>pair.Key)
.GroupBy(x=>new{x.Company,x.Position,x.Age},pair=>pair.Value)
.Select(grp=>grp.ToList())
.ToList();
UPD

但是,如果您需要保留关系Employee->Report,则只需按其键分组即可:

    Dictionary<Employee, Report> data;
    List<List<KeyValuePair<Employee , Report>>> groups = data
                 .GroupBy(x => new { x.Key.Company, x.Key.Position, x.Key.Age })
                 .Select(grp => grp.ToList())
                 .ToList();
字典数据;
列表组=数据
.GroupBy(x=>new{x.Key.Company,x.Key.Position,x.Key.Age})
.Select(grp=>grp.ToList())
.ToList();

如果从字典中获取列表并执行相同操作,该怎么办?f、 GroupBy(x=>new{x.Company,x.Position,x.Age})@Z.R.T.接电话up@Z.R.T.不幸的是,仅对键进行分组就丢失了值。此代码返回列表。它不为员工保留报告。什么是
报告
?您应该提供
员工
类的定义。第三个变量正是我需要的。非常感谢。