【问题标题】:VB.NET How to get Max highest element value from a list that's inside a dictionaryVB.NET 如何从字典中的列表中获取最大元素值
【发布时间】:2015-12-11 02:00:03
【问题描述】:

我有一个字典,它是以字节为键的 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。 我想在 1 行代码中执行此操作,并且使用标准命令而不使用外部库。

是的,它应该返回值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)'.

【问题讨论】:

  • 是的,我看到了那个,它没有帮助我我有一堆列表而不是一个值
  • items.Values.Max(AddressOf Enumerable.Max) 应该可能这样做
  • 是的,做到了..你应该把它写成我会接受的答案。

标签: vb.net list dictionary max


【解决方案1】:

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

items.Values.Max(AddressOf Enumerable.Max)

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多