给一个数字来返回Enum的近似值?

时间:2013-10-31 21:42:39

标签: .net vb.net linq enums numbers

我有这个枚举:

Enum Lame_Bitrate
    kbps_8 = 8
    kbps_16 = 16
    kbps_24 = 24
    kbps_32 = 32
    kbps_40 = 40
    kbps_48 = 48
    kbps_56 = 56
    kbps_64 = 64
    kbps_80 = 80
    kbps_96 = 96
    kbps_112 = 112
    kbps_128 = 128
    kbps_144 = 144
    kbps_160 = 160
    kbps_192 = 192
    kbps_224 = 224
    kbps_256 = 256
    kbps_320 = 320
End Enum

我想在给定数字的情况下返回Enum的近似值。

例如,如果我有数字190,那么我希望在枚举中找到更接近的值,以返回192(枚举的kbps_192值),如果我有数字{ {1}}然后我再次希望返回值196(不返回下一个值192,因为不太近似)。

这样的事情:

224

是否存在任何框架方法,以便在枚举中找到给出其他数字的更近似数字?

我希望你能理解我的问题,谢谢你。

PS:如果可以,我更喜欢使用LINQ扩展的解决方案。

2 个答案:

答案 0 :(得分:4)

如果你想找到最近的枚举:

Dim number = 190
Dim allBitrates() As Lame_Bitrate = DirectCast([Enum].GetValues(GetType(Lame_Bitrate)), Lame_Bitrate())
Dim nearestBitrate = allBitrates.OrderBy(Function(br) Math.Abs(number - br)).First()

如果要查找所有最近的枚举(如果多个具有相同的最小距离):

number = 120 ' two with the same distance
Dim nearestBitrates As IEnumerable(Of Lame_Bitrate) = allBitrates.
    GroupBy(Function(br) Math.Abs(number - br)).
    OrderBy(Function(grp) grp.Key).
    First()
Console.Write(String.Join(",", nearestBitrates))

输出:

kbps_112,kbps_128

答案 1 :(得分:0)

我已经改编了@Tim Schmelter解决方案,并且我已经完成了3个通用功能。

我想分享一下。

·获取最近的枚举值:

' Enum Bitrate As Short : kbps_128 = 128 : kbps_192 = 192 : kbps_256 = 256 : kbps_320 = 320 : End Enum
' MsgBox(Get_Nearest_Enum_Value(Of Bitrate)(133).ToString) ' Result: kbps_128
' MsgBox(Get_Nearest_Enum_Value(Of KnownColor)(1000)) ' Result: 174
Private Function Get_Nearest_Enum_Value(Of T)(ByVal value As Long) As T

    Return [Enum].Parse(GetType(T), [Enum].GetValues(GetType(T)) _
                                          .Cast(Of Object) _
                                          .OrderBy(Function(br) Math.Abs(value - br)) _
                                          .First)

End Function

·获得最近的下限值:

' Enum Bitrate As Short : kbps_128 = 128 : kbps_192 = 192 : kbps_256 = 256 : kbps_320 = 320 : End Enum
' MsgBox(Get_Nearest_Lower_Enum_Value(Of Bitrate)(190).ToString) ' Result: kbps_128
' MsgBox(Get_Nearest_Lower_Enum_Value(Of Bitrate)(196).ToString) ' Result: kbps_192
Private Function Get_Nearest_Lower_Enum_Value(Of T)(ByVal value As Integer) As T

    Select Case value

        Case Is < [Enum].GetValues(GetType(T)).Cast(Of Object).First
            Return Nothing

        Case Else
            Return [Enum].Parse(GetType(T), [Enum].GetValues(GetType(T)) _
                                                  .Cast(Of Object) _
                                                  .Where(Function(enum_value) enum_value <= value) _
                                                  .Last)
    End Select

End Function

·获得最高的枚举值:

' Enum Bitrate As Short : kbps_128 = 128 : kbps_192 = 192 : kbps_256 = 256 : kbps_320 = 320 : End Enum
' MsgBox(Get_Nearest_Higher_Enum_Value(Of Bitrate)(196).ToString) ' Result: kbps_256
' MsgBox(Get_Nearest_Higher_Enum_Value(Of KnownColor)(1000)) ' Result: 0
Private Function Get_Nearest_Higher_Enum_Value(Of T)(ByVal value As Integer) As T

    Select Case value

        Case Is > [Enum].GetValues(GetType(T)).Cast(Of Object).Last
            Return Nothing

        Case Else

            Return [Enum].Parse(GetType(T), [Enum].GetValues(GetType(T)) _
                                  .Cast(Of Object) _
                                  .Where(Function(enum_value) enum_value >= value) _
                                  .First)
    End Select

End Function