C#中字典(dictionary)的问题

marshallzh 2010-09-08 12:52:31
我想在字典中定义如下结构:
Dictionary<int, <int, string>>, 以第一个int为key,<int, string>为value,
请问如何实现。

此外,
对于字典Dictionary<int, string>,我可以通过containValue()找到是否存在某个value
如果这个value存在的话,我想找到它对应的key值,也就是那个int,
请问如何实现呢?给些思路,多谢了!
...全文
1514 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wulala789 2010-09-12
  • 打赏
  • 举报
回复
写个类,实现一下ILIST接口
marshallzh 2010-09-09
  • 打赏
  • 举报
回复
多谢,我基本上就是按照你这种方法写的。

var key = (from k in mydic where string.Compare(k.Value, myString, true) == 0 select k.Key).FirstOrDefault();

[Quote=引用 13 楼 leonbyron 的回复:]

为什么效率不高,Readability 比较差是真的。
Dictionary<int, KeyValuePair<int, string>> dict = ...;
var keys = dict.Where(n => n.value.key == xxx && n.value.value == yyy);
var key = keys.FirstOrDefault();
……
[/Quote]
兔子-顾问 2010-09-08
  • 打赏
  • 举报
回复
这样效率不高,你不如不要用Dictionary了。自己创建一个这样的结构。
leonbyron 2010-09-08
  • 打赏
  • 举报
回复
为什么效率不高,Readability 比较差是真的。
Dictionary<int, KeyValuePair<int, string>> dict = ...;
var keys = dict.Where(n => n.value.key == xxx && n.value.value == yyy);
var key = keys.FirstOrDefault();
粥是稀饭 2010-09-08
  • 打赏
  • 举报
回复
第二个问题比较怪,一般字典类是保证key肯定是唯一的,现在知道value,去找Key,你能保证value唯一么?
例如这样的例子可能就麻烦了,
Dictionary<int,T> dic = new Dictionary<int,T>();
dic.Add(1,T1);
dic.Add(2,T1);

如果你找T1的Key,遍历的话,就有2个。
加油馒头 2010-09-08
  • 打赏
  • 举报
回复
楼上一般都这样实现的
shuai13869896140 2010-09-08
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 zhoufoxcn 的回复:]
引用 6 楼 marshallzh 的回复:
多谢了!
那么第二个问题呢?
对于字典Dictionary<int, string>,我可以通过containValue()找到是否存在某个value
如果这个value存在的话,我想找到它对应的key值,也就是那个int,
请问如何实现呢?
=========
自己编程遍历?


引用 2 楼 wuyq11 的回复:

Dic……
[/Quote]

或者try catch解决```
周公 2010-09-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 marshallzh 的回复:]
多谢了!
那么第二个问题呢?
对于字典Dictionary<int, string>,我可以通过containValue()找到是否存在某个value
如果这个value存在的话,我想找到它对应的key值,也就是那个int,
请问如何实现呢?
=========
自己编程遍历?


引用 2 楼 wuyq11 的回复:

Dictionary<int,T> dic = new……
[/Quote]
Dictionary<int,String> dic = new Dictionary<int,String>();   
if(dic.Keys[key]!=null)
{
//存在key键对应的值
}
else
{
//不存在key键对应的值
}
wulala789 2010-09-08
  • 打赏
  • 举报
回复

public object GetKey(string str)
{
foreach (KeyValuePair<int, string> kp in dic )
{
if (kp.Value != "")
{
return kp.Key;
}
}
return null;
}
  • 打赏
  • 举报
回复
关键是这个value会出现重复否,只取第一个遍历到然后bresk 或者return 就行了,多个值的话整个List也可以的
marshallzh 2010-09-08
  • 打赏
  • 举报
回复
多谢了!
那么第二个问题呢?
对于字典Dictionary<int, string>,我可以通过containValue()找到是否存在某个value
如果这个value存在的话,我想找到它对应的key值,也就是那个int,
请问如何实现呢?
=========
自己编程遍历?

[Quote=引用 2 楼 wuyq11 的回复:]

Dictionary<int,T> dic = new Dictionary<int,T>();
div.Add(1,new T(1,"1"));
class T
{
public int id{get;set;}
public string title{get;set;}
}
[/Quote]
deyygywxf 2010-09-08
  • 打赏
  • 举报
回复
Dictionary<int,T> dic = new Dictionary<int,T>();
div.Add(1,new T(1,"1"));
class T
{
public int id{get;set;}
public string title{get;set;}
}
denbes 2010-09-08
  • 打赏
  • 举报
回复
Dictionary<int, Dictionary<int, string>>
wuyq11 2010-09-08
  • 打赏
  • 举报
回复
foreach (KeyValuePair<string, T> item in dic )
{

}
wuyq11 2010-09-08
  • 打赏
  • 举报
回复
Dictionary<int,T> dic = new Dictionary<int,T>();
div.Add(1,new T(1,"1"));
class T
{
public int id{get;set;}
public string title{get;set;}
}

110,537

社区成员

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

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

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