KeyValuePair
KeyValuePair用法(转)(2012-06-25 10:47:35)
| // 标签: keyvaluepairit | 
C# KeyValuePair<TKey,TValue>的用法。结构体,定义可设置或检索的键/值对。也就是说我们可以通过它记录一个键/值对这样的值。比如我们想定义一个ID(int类型)和Name(string类型)这样的键/值对,那么可以这样使用。
/// 设置键/值对
/// </summary>
/// <returns></returns>
private KeyValuePair<int, string> SetKeyValuePair()
{
int intKey = 1;
string strValue = "My value";
KeyValuePair<int, string> kvp = new KeyValuePair<int, string>(intKey, strValue);
return kvp;
}
/// <summary>
/// 获得键/值对
/// </summary>
private void GetKeyValuePairDemo()
{
    KeyValuePair<int, string> kvp = SetKeyValuePair();
    int intKey = kvp.Key;
    string strValue = kvp.Value;
}
如果想使用泛型的话,也是差不多这样子,一般批量读取数据的时候,当只需要读两个字段(Id and Name)时,如果想不用Model类,并配合泛型使用KeyValuePair,示例:
1、从数据库中读取数据
/// 获取所有企业的Id(enterprise_id)及英文名 (enterprise_name_eng)
/// </summary>
/// <returns>enterprise_info表中的所有企业 Id及英文名</returns>
public List<KeyValuePair<long,string>> GetEnterpriseIdAndNameEngList()
{
//enterprise_id键和enterprise_name_eng 对
List<KeyValuePair<long, string>> lstIdKeyNameEngValue = new List<KeyValuePair<long, string>>();
string cmdText = "select enterprise_id, enterprise_name_eng from enterprise_info";
    using (OracleDataReader reader = OracleHelper.ExecuteReader(OracleHelper.OracleConnString, CommandType.Text, cmdText, null)) 
    {
        try
        {
            MyEventLog.Log.Debug ("cmdText= " + cmdText);
            while (reader.Read())
            {
                KeyValuePair<long, string> idKeyNameEngValue = new KeyValuePair<long, string> (
               &nbs p;    reader.IsDBNull(0) ? 0 : reader.GetInt64(0), 
                 ;    reader.IsDBNull(1) ? string.Empty : reader.GetString(1) 
                 ;    );
                lstIdKeyNameEngValue.Add (idKeyNameEngValue);
            } 
            OracleHelper.DataReaderClose(reader);
        } 
        catch (OracleException e) 
        {
            MyEventLog.Log.Error ("cmdText= " + cmdText);
            MyEventLog.Log.Error(e);
            throw e;
        }
    } 
    return lstIdKeyNameEngValue;
}
2、在业务中处理数据
/// 函数作用:
/// 1、返回从待导入的企业名称中获的有效企业Id集。
/// 2、返回有效的企业行号集。
/// 3、返回无效的企业行号集。
/// </summary>
/// <param name="lstEnterpriseNameEn">待导入的企业名称(英文)集</param>
/// <param name="lstValidRowsIndex">Excel表中有效的企业Id行集</param>
/// <param name="lstInvalidRowsIndex">Excel表中无效的企业Id行集</param>
/// <returns>返回有效的行的索引列表</returns>
public List<long> PrepareForImport(List<string> lstEnterpriseNameEn, out List<int> lstValidRowsIndex, out List<int> lstInvalidRowsIndex)
{
//有效的企业Id行
lstValidRowsIndex = new List<int>();
//无效的企业Id行
lstInvalidRowsIndex = new List<int>(); //获取所有的企业Id及英文名
List<KeyValuePair<long, string>> lstIdKeyNameEngValue = dal.GetEnterpriseIdAndNameEngList(); //用于存放有效的企业的Id,即如果可以在enterprise_info表中找到此企业的英文名,那么表示此企业存在,因此把存在的企业Id获取出来,存放于此变量
List<long> lstValidEnterpriseId = new List<long>(); //通过以下循环可以获得可以有效的企业Id列表
for (int i = 0; i < lstEnterpriseNameEn.Count; i++)
{
foreach (KeyValuePair<long, string> kvp in lstIdKeyNameEngValue)
{
if (lstEnterpriseNameEn[i] == kvp.Value)
{
//获得有效行索引
lstValidRowsIndex.Add(i); //获得有效的企业Id
lstValidEnterpriseId.Add(kvp.Key); //找到了有效的ID后马上跳出内循环,回到外循环
continue;
}
} if (!lstValidRowsIndex.Contains(i) && !lstInvalidRowsIndex.Contains(i))
{
//取得无效行索引
lstInvalidRowsIndex.Add(i);
}
}
return lstValidEnterpriseId;
}
KeyValuePair的更多相关文章
- KeyValuePair<string, string>
		; #region CUP Method /// <summary> /// 请求与响应的超时时间 /// </summary> static public int Timeo ... 
- C#基础总结之五Dictionary<string, string[]>和while循环
		#region 第五天作业 名片集(01) //Dictionary<string, string[]> PersonCard = new Dictionary<string, st ... 
- Dictionary<string, string> 排序
		.net framework 2.0 版 Dictionary<string, string> collection = new Dictionary<string, string& ... 
- 遍历 SortedList<string, string> 中的值(可用于datatable转json)
		SortedList<string, string> STK = new SortedList<string, string>();STK.Add("1", ... 
- Dictionary<string, string>是一个泛型使用说明
		Dictionary<string, string>是一个泛型使用说明 Posted on 2010-08-05 15:03 moss_tan_jun 阅读(2273) 评论(0) 编辑 ... 
- 入门:Java Map<String,String>遍历及修改
		重点:在使用Map时注意key-value,key用于检索value的内容. 在正常情况下,可以不允许重复:在java中分为2中情况,一是内存地址重复,另一个是不同的地址但内容相等. 在使用Map是一 ... 
- 关于   Dictionary<string,string>,和List<T>在View的使用
		在MVC中Dictionary<string,string>如何应用到View页面中呢,例: <input type="text" name=key value= ... 
- alibaba fastjson List<Map<String, String>>2Str
		import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; impo ... 
- getParameterMap()的返回值为Map<String, String[]>,从其中取得请求参数转为Map<String, String>的方法如下:
		直接遍历报错:[Ljava.lang.String;@44739f3f Map<String, String> tempMap = new HashMap<String, Strin ... 
- The constructor User.Student(String, String, String) is not visible
		项目:蒙文词语检索 日期:2016-05-01 提示:The constructor User.Student(String, String, String) is not visible 出处:Db ... 
随机推荐
- Oracle查询每天固定时间段的数据
			select * from GPS_LOG t where to_char(t.gps_time,'hh24:mm:ss')>='15:30:00'and to_char(t.gps_time, ... 
- Oracle 存储过程学习
			转自:http://blog.chinaunix.net/uid-20495387-id-174394.html http://www.cnblogs.com/rootq/articles/11000 ... 
- ubifs核心功能 -- 垃圾回收
			可回收空间的分类 垃圾回收的目的是再利用(回收后的空间大小能写入有效的node),如果再利用的价值越低,其回收的必要性越低.为了进行有效的垃圾回收,UBIFS对可回收空间做了2个层次的水线划分: 死空 ... 
- CDOJ 1431 不是图论 Label:Tarjan || Kosarajn
			Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Description 给出一个nn个点, ... 
- [Leetcode] Permutations II
			Given a collection of numbers that might contain duplicates, return all possible unique permutations ... 
- HDU - Travel
			Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is t ... 
- AngularJS的Filter用法详解
			上一篇讲了自定义Directive,本篇是要讲到AngularJS的Filter. Filter简介 Filter是用来格式化数据用的. Filter的基本原型( '|' 类似于Linux中的管道模式 ... 
- 【BZOJ】2216: [Poi2011]Lightning Conductor
			题意 给一个长度为\(n\)的序列\(a_i\),对于每个\(1 \le i \le n\),找到最小的非负整数\(p\)满足 对于任意的\(j\), \(a_j \le a_i + p - \sqr ... 
- 【BZOJ】1225: [HNOI2001] 求正整数
			http://www.lydsy.com/JudgeOnline/problem.php?id=1225 题意:给一个数n,求一个最小的有n个约数的正整数.(n<=50000) #include ... 
- MySQL实用技巧
			自增Id重新计数 TRUNCATE TABLE 表名 获取最后插入数据的ID SELECT LAST_INSERT_ID(); 使用"id1,id2,id3"当参数 FIN ... 
 
			
		 转载▼
转载▼