2017-09-05 27 views
0

第一次嘗試倉庫模式投字典,但卡住如何返回字典出的Dictionary.Where(條款)如何從Dictionary.Where()在vb.net

Public Function GetWhere(ByVal clause As System.Func(Of myObject, Boolean)) As IRepositoryRead(Of String, myObject) Implements IRepositoryRead(Of String, myObject).GetWhere 
    Return _myDict.Values.Where(clause).ToDictionary(??) 
End Function 

Public Shared Function Filter_OnlyPublic(ByVal SomeMyObject, As myObject) As Boolean 
    Return SomeMyObject.isPublic 
End Function 

_myDict.Values.Where(子句)工作正常。但是如何將結果(內存中查詢)作爲字典返回?

+0

嗨,歡迎來到stackoverflow!您是否介意重新描述您的問題標題,使其更清楚您想要實現的目標? –

回答

0
Return _myDict.Values.Where(clause).ToDictionary(Function (x) x.key) 

{x.key}中的鍵將是myObject中變量的名稱,您希望成爲詞典的鍵。

0

您可以使用類似下面的內容來過濾字典。

Dim dic As New Dictionary(Of String, String) 'Creating new Dictionary object 
dic.Add("Stackoverflow", "somevalue1") 'Adding some items 
dic.Add("Youssef1", "somevalue2") 
dic.Add("Stackoverflow2", "somevalue3") 
dic.Add("Youssef2", "somevalue4") 
Dim newDic = dic.Where(Function(pair) pair.Key.Contains("Stackoverflow")).ToDictionary(Function(pair) pair.Key, Function(pair) pair.Value) 'Filtering the dictionary and assigning it to newDic 
For i = 0 To newDic.Count - 1 'Loop through newDic 
    MessageBox.Show("Key: " & newDic.Keys(i) & Environment.NewLine & "Value: " & newDic.Values(i)) 'Shows the filtered dictionary (newDic) keys & values 
Next