Arrays VB.NET搜索列表中的对象并将找到的对象的值解析为数组
各位编码员 我正在寻找一个简单而快速的解决方案来检索(对象)列表中的一些对象值,然后将这些值合并到二维数组中 让我用代码来解释:Arrays VB.NET搜索列表中的对象并将找到的对象的值解析为数组,arrays,vb.net,linq,find,Arrays,Vb.net,Linq,Find,各位编码员 我正在寻找一个简单而快速的解决方案来检索(对象)列表中的一些对象值,然后将这些值合并到二维数组中 让我用代码来解释: Public Class MyMainClass Dim lstObjects as new list(of TestObject) Sub New() lstObjects.Add(new TestObject With {.IndexPos = 2, .V
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在这里不包含很多值,但在实际应用程序中它有很多属性。希望你能理解我想要实现的目标,并能在这方面帮助我。我花了几个小时在谷歌上寻找解决方案…使用Linq处理多维数组并不容易。请看关于这方面的更大讨论 在这种情况下,我会选择好的旧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
在瓦舍尔的所有好建议之后,我终于做了这个,威奇的工作速度很快,正是我想要的 PS.Als你可以看到我比较LINQ中的数组,这是因为我无法将is更改为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
“”
''存储所有输入仪表值
'''
私有intInputMeters(,)作为整数
子集合ArraySize(InputMeters()为整数)
'重拨仪表值的返回整数数组'
ReDim输入米数(输入米数.长度,1)
端接头
公共函数GetInputMeters()为整数(,)
“搜索马奇的来源,一旦我们找到了,就把它还给我们。”。
Dim y As IEnumerable(Of Source)=Sources.Where(函数(a)intInputMeterRequest.Contains(a.ChannelNumber)).aspallel
'现在将值传递给数组
对于i,整数=0到y。计数-1
输入米数(i,0)=y(i)。米值左
输入米数(i,1)=y(i)。米值右
下一个
'将新填充的数组返回给用户
返回输入计
端函数
两个人同心协力;-)我有这个确切的密码。这是可行的,除了第一个LINQ查询没有给我任何结果。我仍然在研究如何将搜索数组传递到LINQ查询并获得匹配结果……这可能是因为您将IndexPos
与所有搜索值进行比较。这些都是不同的(Int32
与Array-of-Int32)
)。我想你想要的是一种包含搜索。要做到这一点,请将您的SearchValues
放入如下列表:Dim searchList As list(Of Int32)=SearchValues.ToList()
。然后,您可以检查如下内容:Sources.Where(函数(a)searchList.Contains(a.IndexPos))
太好了,这正是我想要的。最后,我使用了以下命令:Dim y作为IEnumerable(Of TestObject)=lstObjects.Where(函数(a)SearchValues.Contains(a.IndexPos))intResultSize=y.Count-1 ReDim intInputMeters(intResultSize,1)作为整数=0,以intResultSize结果(I,0)=y(I).Value1结果(i,1)=y(i)。Value2下一个返回结果
难以读取,但看起来不错。你的问题有这个答案吗?是的,我会把它作为答案贴出来。(除非有人有更快/更干净的解决方案?)谢谢!:-)使用字典
对你有用吗?看起来你正试图在列表
上进行字典查找,这必然会降低效率--列表
中的查找是线性时间,而字典
中的查找是恒定时间,只要它的散列合理。嘿,Craig,不幸的是,这不是因为对象有更多的值。例如,本示例仅公开了其中三个。谢谢你和我一起思考。