VB.NET如何从字典中的列表中获取Max最高元素值

时间:2015-09-14 23:29:52

标签: vb.net list dictionary max

我得到了一个字典,它是Byte的KeyValuePair作为键,而Uintegers列表是值

Dim items As New Dictionary(Of Byte, List(Of UInteger))
items.Add(1, New List(Of UInteger)(New UInteger() {6, 68, 238, 768, 23484, 23, 7573, 12737, 76579}))
items.Add(54, New List(Of UInteger)(New UInteger() {68768, 2347, 1237, 76878, 1238, 57882391}))
items.Add(5, New List(Of UInteger)(New UInteger() {66, 787, 2883, 1189, 5, 934, 4568, 199999, 500000}))

我想通过检查字典中的所有条目来提取最大的UInteger。 我希望在一行代码中使用标准命令而不使用外部库。

是的它应该返回值57882391

这是我到目前为止所尝试的内容。

Dim TheBiggestUInt as UInteger = items.Where(Function(x As List(Of UInteger)) x.Item = items.Max(Function(y As List(Of UInteger)) y.Item))
Dim TheBiggestUInt as UInteger = items.Where(Function(x As List(Of UInteger)) x = items.Max(Function(y As List(Of UInteger)) y))
Dim TheBiggestUInt as UInteger = items.Values.Where(Function(x As List(Of UInteger)) x = items.Max(Function(y As List(Of UInteger)) y)) 



    我收到一个错误

Overload resolution failed because no accessible 'Where' can be called with these arguments:
    Extension method 'Public Function Where(predicate As System.Func(Of System.Collections.Generic.KeyValuePair(Of Byte, System.Collections.Generic.List(Of UInteger)), Integer, Boolean)) As System.Collections.Generic.IEnumerable(Of System.Collections.Generic.KeyValuePair(Of Byte, System.Collections.Generic.List(Of UInteger)))' defined in 'System.Linq.Enumerable': Nested function does not have a signature that is compatible with delegate 'System.Func(Of System.Collections.Generic.KeyValuePair(Of Byte, System.Collections.Generic.List(Of UInteger)), Integer, Boolean)'.
    Extension method 'Public Function Where(predicate As System.Func(Of System.Collections.Generic.KeyValuePair(Of Byte, System.Collections.Generic.List(Of UInteger)), Boolean)) As System.Collections.Generic.IEnumerable(Of System.Collections.Generic.KeyValuePair(Of Byte, System.Collections.Generic.List(Of UInteger)))' defined in 'System.Linq.Enumerable': Overload resolution failed because no accessible 'Max' can be called with these arguments:
    Extension method 'Public Function Max(Of System.Collections.Generic.List(Of UInteger))(selector As System.Func(Of System.Collections.Generic.KeyValuePair(Of Byte, System.Collections.Generic.List(Of UInteger)), System.Collections.Generic.List(Of UInteger))) As System.Collections.Generic.List(Of UInteger)' defined in 'System.Linq.Enumerable': Nested function does not have a signature that is compatible with delegate 'System.Func(Of System.Collections.Generic.KeyValuePair(Of Byte, System.Collections.Generic.List(Of UInteger)), System.Collections.Generic.List(Of UInteger))'.
    Extension method 'Public Function Max(selector As System.Func(Of System.Collections.Generic.KeyValuePair(Of Byte, System.Collections.Generic.List(Of UInteger)), Decimal?)) As Decimal?' defined in 'System.Linq.Enumerable': Value of type 'System.Collections.Generic.List(Of UInteger)' cannot be converted to 'Decimal?'.
    Extension method 'Public Function Max(selector As System.Func(Of System.Collections.Generic.KeyValuePair(Of Byte, System.Collections.Generic.List(Of UInteger)), Decimal)) As Decimal' defined in 'System.Linq.Enumerable': Value of type 'System.Collections.Generic.List(Of UInteger)' cannot be converted to 'Decimal'.
    Extension method 'Public Function Max(selector As System.Func(Of System.Collections.Generic.KeyValuePair(Of Byte, System.Collections.Generic.List(Of UInteger)), Double?)) As Double?' defined in 'System.Linq.Enumerable': Value of type 'System.Collections.Generic.List(Of UInteger)' cannot be converted to 'Double?'.
    Extension method 'Public Function Max(selector As System.Func(Of System.Collections.Generic.KeyValuePair(Of Byte, System.Collections.Generic.List(Of UInteger)), Double)) As Double' defined in 'System.Linq.Enumerable': Value of type 'System.Collections.Generic.List(Of UInteger)' cannot be converted to 'Double'.
    Extension method 'Public Function Max(selector As System.Func(Of System.Collections.Generic.KeyValuePair(Of Byte, System.Collections.Generic.List(Of UInteger)), Single?)) As Single?' defined in 'System.Linq.Enumerable': Value of type 'System.Collections.Generic.List(Of UInteger)' cannot be converted to 'Single?'.
    Extension method 'Public Function Max(selector As System.Func(Of System.Collections.Generic.KeyValuePair(Of Byte, System.Collections.Generic.List(Of UInteger)), Single)) As Single' defined in 'System.Linq.Enumerable': Value of type 'System.Collections.Generic.List(Of UInteger)' cannot be converted to 'Single'.
    Extension method 'Public Function Max(selector As System.Func(Of System.Collections.Generic.KeyValuePair(Of Byte, System.Collections.Generic.List(Of UInteger)), Long?)) As Long?' defined in 'System.Linq.Enumerable': Value of type 'System.Collections.Generic.List(Of UInteger)' cannot be converted to 'Long?'.
    Extension method 'Public Function Max(selector As System.Func(Of System.Collections.Generic.KeyValuePair(Of Byte, System.Collections.Generic.List(Of UInteger)), Long)) As Long' defined in 'System.Linq.Enumerable': Value of type 'System.Collections.Generic.List(Of UInteger)' cannot be converted to 'Long'.
    Extension method 'Public Function Max(selector As System.Func(Of System.Collections.Generic.KeyValuePair(Of Byte, System.Collections.Generic.List(Of UInteger)), Integer?)) As Integer?' defined in 'System.Linq.Enumerable': Value of type 'System.Collections.Generic.List(Of UInteger)' cannot be converted to 'Integer?'.
    Extension method 'Public Function Max(selector As System.Func(Of System.Collections.Generic.KeyValuePair(Of Byte, System.Collections.Generic.List(Of UInteger)), Integer)) As Integer' defined in 'System.Linq.Enumerable': Value of type 'System.Collections.Generic.List(Of UInteger)' cannot be converted to 'Integer'.

我最好的尝试,错误最少

Dim TheBiggestUInt as UInteger = items.Values.Where(Function(x As List(Of UInteger)) x = items.Values.Max(Function(y As List(Of UInteger)) y))

Overload resolution failed because no accessible 'Where' can be called with these arguments:
    Extension method 'Public Function Where(predicate As System.Func(Of System.Collections.Generic.List(Of UInteger), Integer, Boolean)) As System.Collections.Generic.IEnumerable(Of System.Collections.Generic.List(Of UInteger))' defined in 'System.Linq.Enumerable': Nested function does not have a signature that is compatible with delegate 'System.Func(Of System.Collections.Generic.List(Of UInteger), Integer, Boolean)'.
    Extension method 'Public Function Where(predicate As System.Func(Of System.Collections.Generic.List(Of UInteger), Boolean)) As System.Collections.Generic.IEnumerable(Of System.Collections.Generic.List(Of UInteger))' defined in 'System.Linq.Enumerable': Operator '=' is not defined for types 'System.Collections.Generic.List(Of UInteger)' and 'System.Collections.Generic.List(Of UInteger)'.

1 个答案:

答案 0 :(得分:4)

您想要从每个内部列表中找到最大值;你可以先获得每个内部列表的最大值,然后获得这些最大值的最大值。

items.Values.Max(AddressOf Enumerable.Max)

使用lambda而不是方法组的替代语法:

items.Values.Max(Function(innerList) innerList.Max)