var dic = new Dictionary<string, int>() { {"a",1}, {"b",2}, {"c",3}, {"d",1} }; foreach (var item in dic) { if (itemValue == 1) { ConsoleWriteLine(itemKey); } }
1最直白的循环遍历方法,可以分为遍历key--value键值对以及所有的key两种表现形式
2用Linq的方式去查询(当然了这里要添加对应的命名空间 using SystemLinq)
如下为一个十分简单的代码示例:
private void GetDicKeyByValue()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dicAdd("1", "1");
dicAdd("2", "2");
dicAdd("3", "2");
//foreach KeyValuePair traversing
foreach (KeyValuePair<string, string> kvp in dic)
{
if (kvpValueEquals("2"))
{
// kvpKey;
}
}
//foreach dicKeys
foreach (string key in dicKeys)
{
if (dic[key]Equals("2"))
{
// key
}
}
//Linq
var keys = dicWhere(q => qValue == "2")Select(q => qKey); //get all keys
List<string> keyList = (from q in dic
where qValue == "2"
select qKey)ToList<string>(); //get all keys
var firstKey = dicFirstOrDefault(q => qValue == "2")Key; //get first key
}
使用input标签的value属性可以获取input的value值。
1、新建html文件,在body标签中添加input标签,为input标签设置value属性,属性值为“demo”:
2、为了方便获取input元素,为input标签设置id属性,属性值为“ipt”:
3、添加script标签,在script标签中添加代码“alert(documentgetElementById('ipt')value); ”,其中alert的意思是d窗,alert里面的内容是获取input标签中的value值代码:
<#--
/
$Id: checkboxlistftl 720258 2008-11-24 19:05:16Z musachy $
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements See the NOTICE file
distributed with this work for additional information
regarding copyright ownership The ASF licenses this file
to you under the Apache License, Version 20 (the
"License"); you may not use this file except in compliance
with the License You may obtain a copy of the License at
>
以上就是关于C#如何通过Key键获取Dictionary集合内的Value全部的内容,包括:C#如何通过Key键获取Dictionary集合内的Value、怎么获取input的value值、FreeMarker如何取得字符串的长度等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)