如何从字典返回密钥? [英] How to return key from Dictionary?

查看:86
本文介绍了如何从字典返回密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天

我创建了字典.字典值来自存储过程.


以下值存储在我的字典中:

Good day

I created a dictionary. The dictionary values is coming from a stored procedure.


The following values are stored inside my dictionary:

VariableDescription           VariableValue

Version                       1.0.0.0 



字典键是VariableValue,值是VariableDescription.

我想返回VariableValue,"1.0.0.0",而不是整个字典.这是我编写的代码:



The dictionary key is VariableValue and the value is VariableDescription.

I want to return the VariableValue, "1.0.0.0" and not the whole dictionary. Here is my code that I have written:

public Dictionary<string, string> SelectdicVersion()
{       
Dictionary<string, string> dicVersion = new Dictionary<string, string>();
    {
     SqlDatabase sqldb = new SqlDatabase(connectionstring);
     IDataReader reader = sqldb.ExecuteReader("pSEL_Version", new object[] { });
           while (reader.Read())
          {
dicVersion.Add(reader["VariableValue"].ToString(), 
(reader["VariableDescription"].ToString()));
sqldb = null;
            }

       }
 return dicVersion;
 }



目前,它正在返回整个字典.我必须更改什么才能返回"1.0.0.0"?



At the moment it is returning the whole dictionary. What must I change so that "1.0.0.0" can be returned?

推荐答案

似乎您正在使用存储过程.如果您只寻找版本(reader["VariableValue"].ToString()),则在阅读信息时确实不需要词典.您只需要检查每个记录读取的键,并在找到版本时中止.找到版本"时,返回reader["VariableDescription"].ToString()).理想情况下,您应该创建存储过程,以仅允许选择单个键,而不是整个字典.
It appears that you are using a stored proceedure. You really do not need the dictionary when reading the information if all you are looking for is the Version (reader["VariableValue"].ToString()). You just have to check the key for each record read and abort when you find the version. When you find "Version", return the reader["VariableDescription"].ToString()). Ideally you should create the stored procedure to allow selection of only a single key, not the entire dictionary.


dicVersion.containsValue("VariableValue);将为您提供"VariableValue"的键.
dicVersion.containsValue("VariableValue); will give you the key for "VariableValue".


感谢大家的帮助.我更改了Short过程,使其只能返回一个值,然后使用ExecuteScalar.

Thanx for everybody''s help. I changed my Short procedure that it must only return one value, and I used ExecuteScalar.

public string SelectdicVersion()
        {
            
SqlDatabase sqldb = new SqlDatabase(connectionstring);
string reader = sqldb.ExecuteScalar("pSEL_Version", new object[] { }).ToString();
return reader;
         }


这篇关于如何从字典返回密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆