【发布时间】:2019-03-11 20:01:37
【问题描述】:
假设我有一个字典wipProfile,其定义为:
Dictionary<string, int> wipProfile = new Dictionary<string, int>() { { "L1", 10 }, { "L2", 12 }, { "L3", 23 }, { "L4", 9 } };
我想知道如何只遍历那些值 >= 10 的对象?
我知道我可以这样做:
foreach (KeyValuePair<string, int> lot in wipProfile)
{
if (lot.Value >= 10)
{
//Do something here.
}
}
但是有没有办法在进入循环之前指定过滤条件,这样我就不需要遍历整个字典了,这样说:
foreach (KeyValuePair<string, int> lot in wipProfile where lot.Value >= 10){
//...
}
【问题讨论】:
-
那么,您认为
foreach和您对so that I don't need to iterate through the entire dictionary的想法有什么区别? -
@RandRandom 主要是更简化的代码。而且我只能处理那些我真正关心的人。
标签: c# loops dictionary foreach