0

私はこれを for ループの原因で実行できることを知っています。それが私が今やっている方法です。タスクを達成するためのより効率的な方法を望んでいました。

私は辞書(整数、ブール値)または文字列、ブール値を持っています。すべての値が true (またはその時点で必要なものに応じて false) である辞書から、(整数の) リストまたは文字列のリストを取得したい

それを一般化するか、「ブラックボックス」にするために、それは任意の辞書(何でも、何でも)であり、値がその時点で探しているものであるリスト(何でも)を返します。

string, string where value = "Closed"

要するに:私は価値があるすべてのキーのすべてのリストが欲しい=いくつかの基準

私の現在のコード:

    Public Function FindInDict(Of tx, ty)(thedict As Dictionary(Of tx, ty), criteria As ty) As List(Of tx)
    Dim tmpList As New List(Of tx)

    For xloop As Integer = 0 To thedict.Count - 1
        If CType(thedict(thedict.Keys(xloop)), ty).Equals(criteria) Then
            tmpList.Add(thedict.Keys(xloop))
        End If
    Next
    Return tmpList
End Function
4

2 に答える 2

2

これは、Linq を使用して簡単に行うことができます。

Public Function FindInDict(Of tx, ty)(thedict As Dictionary(Of tx, ty), criteria As ty) As List(Of tx)
    Return (From kvp In thedict
            Where kvp.Value.Equals(criteria)
            Select kvp.key).ToList()
End Function
于 2012-04-24T01:51:55.937 に答える
1

次のように LINQ を使用します。

Dim tStorage As Dictionary(Of String, String) = New Dictionary(Of String, String)
Dim tKeys As List(Of String) = New List(Of String)
Dim tCriteria As List(Of String) = New List(Of String)

tStorage.Add("One", "Uno")
tStorage.Add("Two", "Dos")
tStorage.Add("Three", "Tres")
tStorage.Add("Four", "Quatro")

tCriteria.Add("Dos")
tCriteria.Add("Quatro")

tKeys = (From k In tStorage.Keys Where tCriteria.Contains(tStorage(k)) Select k).ToList

For Each tKey As String In tKeys
    Console.WriteLine(tKey)
Next

Console.ReadKey()
于 2012-04-23T21:14:18.313 に答える