7

これは辞書です、

Dictionary<string, uint> oSomeDictionary = new Dictionary<string, uint>();

oSomeDictionary.Add("dart1",1);
oSomeDictionary.Add("card2",1);
oSomeDictionary.Add("dart3",2);
oSomeDictionary.Add("card4",0);
oSomeDictionary.Add("dart5",3);
oSomeDictionary.Add("card6",1);
oSomeDictionary.Add("card7",0);

oSomeDictionary文字列「カード」で始まり、ゼロより大きい値を持つキーからキーと値のペアを取得する方法は?

4

5 に答える 5

10
var result = oSomeDictionary.Where(r=> r.Key.StartsWith("card") && r.Value > 0);

出力用:

foreach (var item in result)
{
    Console.WriteLine("Key: {0}, Value: {1}", item.Key, item.Value);
}

出力:

Key: card2, Value: 1
Key: card6, Value: 1

含めることを忘れないでくださいusing System.Linq

于 2013-04-01T10:26:32.707 に答える
3

Enumerable.Whereを使用して辞書要素をフィルタリングできます

var result = oSomeDictionary.Where(c=>c.Key.StartsWith("card")  && c.Value > 0)
于 2013-04-01T10:26:52.943 に答える
2

次のような方法IEnumerable.Where()を使用できます。String.StartsWith()

Dictionary<string, uint> oSomeDictionary = new Dictionary<string, uint>();

oSomeDictionary.Add("dart1", 1);
oSomeDictionary.Add("card2", 1);
oSomeDictionary.Add("dart3", 2);
oSomeDictionary.Add("card4", 0);
oSomeDictionary.Add("dart5", 3);
oSomeDictionary.Add("card6", 1);
oSomeDictionary.Add("card7", 0);

var yourlist = oSomeDictionary.Where(n => n.Key.StartsWith("card") && n.Value > 0);

foreach (var i in yourlist)
{
    Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value);
}

出力は次のようになります。

Key: card2, Value: 1
Key: card6, Value: 1

ここに がありDEMOます。

于 2013-04-01T10:27:47.023 に答える