2017-09-05 4 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

こんにちは。あなたが達成しようとしていることをより明確にするために、あなたの質問のタイトルを言い換えてもよろしいですか? –

答えて

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