使用dictionary &元组

本文关键字:元组 dictionary 使用 | 更新日期: 2023-09-27 18:18:57

我有一个像这样的c#结构

Dictionary<Tuple<int, int>, Tuple<DateTime, string>> tmp = new Dictionary<Tuple<int, int>, Tuple<DateTime, string>>();

第一个元组接受一个复合键empid, deptid。第二个元组是hiredate &emp名称

字典中的第一条记录是这样的:

DateTime dt = "2011-01-02 00:00:00.000";//hiredate of employee
tmp.Add(new Tuple<int, int>(Convert.ToInt32(empid), Convert.ToInt32(deptid)), new Tuple<DateTime, string>(dt, "AMy"));

字典中的第二条记录

DateTime dt2 = "2011-09-09 00:00:00.000";//hiredate of employee
    tmp.Add(new Tuple<int, int>(Convert.ToInt32(anotherempid), Convert.ToInt32(deptid)), new Tuple<DateTime, string>(dt2, "Cathy"));

字典中有几个这样的条目。我怎样用这本字典找到每个系的最大日期呢?

谢谢先生

使用dictionary &元组

应该这样做:

var result = tmp
            .GroupBy(x => x.Key.Item2)
            .Select(g => new
            {
                DepartmentId = g.Key,
                MaxDate = g.Max(x => x.Value.Item1)
            });

,但我会使用自定义类而不是Tuple