2013-11-27 47 views
0

有沒有辦法調用列表的特定列表,而不必檢查(或循環)它們全部?調用列表項var =值

更容易用一個例子來理解....

可以說

callList(5).key = "1234" 
callList(5).callOpened = "11/26/13" 

現在我想這樣做

textbox_callOpened.text = callList(where key = "1234").callOpened 

我還需要知道什麼索引那是因爲我還需要輸出更多的項目。

回答

0

您可以使用LINQ。在文件的頂部添加Imports System.Linq和使用Firstlambda expression作謂語得到你需要的東西:

' that gives you item matching your predicate ' 
Dim item = callList.First(Function(x) x.Key = "1234") 

' you can use it to set the property ' 
textbox_callOpened.text = item.callOpened 
+0

不要忘了如果找不到空引用 – Fredou

+1

如果找不到元素,它將不會是'NullReferenceException'。它將是'InvalidOperationException'。 – MarcinJuraszek

+0

爲真,使用「第一個」,如果沒有找到,它將不會返回空對象。我忘了那個。 – Fredou

0

僞代碼,沒有測試,但應該工作

dim something = callList.firstordefault(function(d) d.key = "1234") 

    if something is not nothing then 
     textbox_callOpened.text = something.callOpened 
    else 
     'cant find an element with key 1234 
    end if 
+0

能否請您解釋一下嗎?我不明白的 功能(d)d.key 現在我做這樣的事情.... 昏暗一個作爲整數 對於i = 0到calllist.Count 如果呼叫清單(I).KEY =「1234」Then a = i End If Next – Ishey4

+0

@ Ishey4,我不會試着解釋lambda如何工作,但我會給你這個鏈接解釋它; http://msdn.microsoft.com/en-us/library/vstudio/bb531253.aspx – Fredou

+0

哇!我將不得不通過梳子...看起來像很多信息那裏... ty 我把它帶回..沒有那麼多...它似乎有點像一個迷你功能或子加 – Ishey4