我有以下字典:
Dim idQueuedJobs As IDictionary(Of Int32, KeyValuePair(Of String, Int32)) = New Dictionary(Of Int32, KeyValuePair(Of String, Int32))
为什么重复KeyValue对?这是因为第一个 Int32 只是一个普通索引,字符串后跟另一个 Int32 包含系统名称和qued作业的优先级 idQueuedJobs
我想计算 idQueuedJobs 中系统名称的总数,如何使用groupby方法获取此计数?
这样的事可能吗?
numberOfSystems As Int32 = idQueuedJobs.Values.GroupBy(...)
答案 0 :(得分:0)
idQueuedJobs.Add(1, New KeyValuePair(Of String, Int32)("SYSTEM1", 1))
idQueuedJobs.Add(2, New KeyValuePair(Of String, Int32)("SYSTEM1", 2))
idQueuedJobs.Add(3, New KeyValuePair(Of String, Int32)("SYSTEM1", 1))
idQueuedJobs.Add(4, New KeyValuePair(Of String, Int32)("SYSTEM1", 3))
idQueuedJobs.Add(5, New KeyValuePair(Of String, Int32)("SYSTEM1", 4))
idQueuedJobs.Add(6, New KeyValuePair(Of String, Int32)("SYSTEM2", 1))
idQueuedJobs.Add(7, New KeyValuePair(Of String, Int32)("SYSTEM2", 2))
idQueuedJobs.Add(8, New KeyValuePair(Of String, Int32)("SYSTEM3", 3))
idQueuedJobs.Add(9, New KeyValuePair(Of String, Int32)("SYSTEM3", 3))
idQueuedJobs.Add(10, New KeyValuePair(Of String, Int32)("SYSTEM3", 4))
Dim numberOfSystems As Int32 = idQueuedJobs.Values.GroupBy(Of String)(Function(p) p.Key).Count
以下代码将导致 numberOfSystems = 3