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