【问题标题】:VB.NET Search for objects in list and parse values of found object to arrayVB.NET 在列表中搜索对象并将找到的对象的值解析为数组
【发布时间】:2017-06-16 07:37:36
【问题描述】:

各位程序员,

我正在寻找一种简单快速的解决方案来检索 List(of Object) 中的一些对象值,然后将这些值组合到一个二维数组中。

让我用代码来解释一下:

Public Class MyMainClass
   Dim lstObjects as new list(of TestObject)

   Sub New()
      lstObjects.Add(new TestObject With {.IndexPos = 2,
                                          .Value1 = 4578,
                                          .Value2 = 9876234)

      lstObjects.Add(new TestObject With {.IndexPos = 8,
                                          .Value1 = 45232378,
                                          .Value2 = 98761111234)

      RetrieveValues(New Integer() {1, 4, 8}
   End Sub


   Function RetrieveValues(SearchValues()) as integer (,)

     Dim y As IEnumerable(Of TestObject) = Sources.Where(Function(a) a.IndexPos .Equals(SearchValues))

    'Then do something here to convert Value1 and Value 2 of the object to a two dimensional array

   End function
End Class


Public Class TestObject
   Public IndexPos As Integer
   Public Value1 As Integer
   Public Value2 As Integer
End Class

代码是一个示例,未经测试(仅在此编辑器中编写)。 TestObject 在这里不包含很多值,但在实际应用中它有很多属性。希望您了解我正在努力实现的目标,并可以帮助我解决这个问题。我在谷歌上花了几个小时寻找解决方案......

【问题讨论】:

    标签: arrays vb.net linq find


    【解决方案1】:

    使用 Linq 处理多维数组并不容易。请参阅有关此here 的更大讨论。

    在这种情况下,我会使用旧的 VB.NET 并遍历变量 y 中已有的项目 - 但是,我会在多次访问之前将其放入列表中。

    Dim items = y.ToList()
    Dim result(items.Count, 2) as Integer ' god I hate that syntax
    
    For itemIndex As Integer = 0 To items.Count - 1
        result(itemIndex, 0) = items(itemIndex).Value1
        result(itemIndex, 1) = items(itemIndex).Value2
    Next
    

    【讨论】:

    • 两个人一个想法 ;-) 我有这个确切的代码。哪个可以工作,除了第一个 LINQ 查询没有给我任何结果。我仍然对如何将搜索数组传递到 LINQ 查询并获得匹配结果感到困惑......
    • 这可能是因为您将IndexPos 与所有搜索值进行了比较。这些永远不会相同(Int32Array-of-Int32))。我认为您想要的是一种包含搜索。为了能够做到这一点,请将您的 SearchValues 放入如下列表:Dim searchList As List(Of Int32) = SearchValues.ToList()。然后你可以像这样检查包含:Sources.Where(Function(a) searchList.Contains(a.IndexPos))
    • 太好了,这正是我想要的。我最终使用了这个:Dim y As IEnumerable(Of TestObject) = lstObjects.Where(Function(a) SearchValues.Contains(a.IndexPos)) intResultSize = y.Count - 1 ReDim intInputMeters(intResultSize, 1) For i As Integer = 0 To intResultSize result(i, 0) = y(i).Value1 result(i, 1) = y(i).Value2 Next Return result
    • 难读但看起来不错。你的问题得到回答了吗?
    • 是的,我会把它作为答案发布。 (除非有人有更快/更清洁的解决方案?)谢谢! :-)
    【解决方案2】:

    在 Waescher 的所有好建议之后,我终于做出了这个,它的工作速度很快,完全符合我的要求。

    PS。你也可以看到我比较了 LINQ 中的数组,这是因为我无法更改为 T 的列表,正如 Waescher 前面提到的那样。

    ''' <summary>
    ''' Stores all the input meter values
    ''' </summary>
    Private intInputMeters(,) As Integer
    
    Sub SetArraySize(InputMeters() As Integer)
            'Redim the return integer array's for the meter values
            ReDim intInputMeters(InputMeters.Length, 1)
    End sub
    
    
    Public Function GetInputMeters() As Integer(,)
    
        'Search for the machting sources and return the once we found.
        Dim y As IEnumerable(Of Source) = Sources.Where(Function(a) intInputMeterRequest.Contains(a.ChannelNumber)).AsParallel
    
        'Now pass the values to the array
        For i As Integer = 0 To y.Count - 1
            intInputMeters(i, 0) = y(i).MeterValueLeft
            intInputMeters(i, 1) = y(i).MeterValueRight
        Next
    
        'Return the new filled array to the user
        Return intInputMeters
    End Function
    

    【讨论】:

    • 使用Dictionary 对您有用吗?看起来您正在尝试在 List 上进行字典查找,这必然会降低效率 --- 在 List 中查找是线性时间,在 Dictionary 中查找是恒定时间,前提是它被合理地散列好吧。
    • 嘿 Craig,不幸的是不是因为对象有更多的值。例如,只是暴露了其中的三个。感谢您与我一起思考。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多