这是一本字典,

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中获取以字符串“card”开头且值大于零的键/值对?

最佳答案

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

08-28 00:02