使用词典&获得最近雇用的员工元组

时间:2014-08-20 08:39:56

标签: c# dictionary

我有一个在C#中看起来像这样的结构

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

1st Tuple采用了empid,deptid的复合键。 第二元组是雇佣的&amp; 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"));

字典中有几个这样的条目。如何使用此词典查找每个部门的maxhiredate?

由于 MR

1 个答案:

答案 0 :(得分:0)

这样的事情应该有效:

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

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

相关问题