2013-08-06 2 views
0

다른 눈 쌍이 필요합니다. 나는 정규 표현식으로 Hashtable을 스캔하기 위해이 LINQ 문법으로 놀아왔다. 그것을 올바르게 이해하지 못하는 것 같습니다. 목표는 모든 키를 정규 표현식과 일치시킨 다음 그 결과를 사용하여 나머지 값을 별도의 정규 표현식과 일치시킵니다. 아래의 테스트 케이스에서 나는 처음 세 항목으로 끝나야한다.Linq with HashTable Matching

Private ReadOnly Property Testhash As Hashtable 
    Get 
     Testhash = New Hashtable 
     Testhash.Add("a1a", "abc") 
     Testhash.Add("a2a", "aac") 
     Testhash.Add("a3a", "acc") 
     Testhash.Add("a4a", "ade") 
     Testhash.Add("a1b", "abc") 
     Testhash.Add("a2b", "aac") 
     Testhash.Add("a3b", "acc") 
     Testhash.Add("a4b", "ade") 
    End Get 
End Property 

Public Sub TestHashSearch() 

    Dim KeyPattern As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("a.a") 
    Dim ValuePattern As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("a.c") 

    Try 
     Dim queryMatchingPairs = (From item In Testhash 
            Let MatchedKeys = KeyPattern.Matches(item.key) 
            From key In MatchedKeys 
            Let MatchedValues = ValuePattern.Matches(key.value) 
            From val In MatchedValues 
            Select item).ToList.Distinct 

     Dim info = queryMatchingPairs 

    Catch ex As Exception 

    End Try 

End Sub 

답변

1

키와 값을 동시에 일치시킬 수 없습니까?

Dim queryMatchingPairs = (From item In Testhash 
          Where KeyPattern.IsMatch(item.Key) And ValuePattern.IsMatch(item.Value) 
          Select item).ToList 
+1

확실히 더 우아! – dgp

0

나는 조금 더 일찍 쉬어야했다. 올바른 솔루션은 두 번째 정규 표현식에서 "from item"을 사용하지 않고 "from key"를 사용하지 않습니다. 또한 해시 테이블에는 "distinct"가 필요하지 않습니다.

  Dim queryMatchingPairs = (From item In Testhash 
             Let MatchedKeys = KeyPattern.Matches(item.key) 
             From key In MatchedKeys 
             Let MatchedValues = ValuePattern.Matches(item.value) 
             From val In MatchedValues 
             Select item).ToList