关于Dictionary的问题,我想判断里面的KEY值是否大于10的

WILSON SHUN 2018-06-23 09:05:50
我想判断的是大于这个KEY的,各位大神有办法吗?
Dictionary<int, string> ports = new Dictionary<int, string>();
ports.Add(1, port);
ports.Add(2, port);
......
ports.Add(100, port);
...全文
249 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
WILSON SHUN 2018-06-23
  • 打赏
  • 举报
回复
引用 2 楼 xuzuning 的回复:
不要事后的 我想!
你在选择一种数据结构时,就应知道后续工作该如何进行
也就是说:正因为这种结构适合进行后续工作,所以我才选择了他
            Dictionary<int, string> ports = new Dictionary<int, string>();
ports.Add(1, "port1");
ports.Add(2, "port2");
ports.Add(100, "port100");
foreach (var x in ports.Where(t => t.Key > 10))
{
Console.WriteLine("{0} => {1}", x.Key, x.Value);
}
Console.WriteLine("---");
ports = ports.Where(t => t.Key <= 10).ToDictionary(k=>k.Key,v=>v.Value);
foreach (var x in ports)
{
Console.WriteLine("{0} => {1}", x.Key, x.Value);
}

谢谢大神,主要是我没用这个,别人用的这个方法,我就用了
xuzuning 2018-06-23
  • 打赏
  • 举报
回复
不要事后的 我想!
你在选择一种数据结构时,就应知道后续工作该如何进行
也就是说:正因为这种结构适合进行后续工作,所以我才选择了他
            Dictionary<int, string> ports = new Dictionary<int, string>();
ports.Add(1, "port1");
ports.Add(2, "port2");
ports.Add(100, "port100");
foreach (var x in ports.Where(t => t.Key > 10))
{
Console.WriteLine("{0} => {1}", x.Key, x.Value);
}
Console.WriteLine("---");
ports = ports.Where(t => t.Key <= 10).ToDictionary(k=>k.Key,v=>v.Value);
foreach (var x in ports)
{
Console.WriteLine("{0} => {1}", x.Key, x.Value);
}
WILSON SHUN 2018-06-23
  • 打赏
  • 举报
回复
再补充一个问题,满足这个条件之后我想删除这条value和KEY,怎么删除?

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧