C# KeyValuePair<TKey,TValue>的用法-转载
C# KeyValuePair<TKey,TValue>的用法。结构体,定义可设置或检索的键/值对。也就是说我们可以通过 它记录一个键/值对这样的值。比如我们想定义一个ID(int类型)和Name(string类型)这样的键/值对,那么可以这 样使用。
/// <summary>
/// 设置键/值对
/// </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、从数据库中读取数据
/// <summary>
/// 获取所有企业的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、在业务中处理数据
/// <summary>
/// 函数作用:
/// 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;
}
来源:http://www.cnblogs.com/mayanshuang/archive/2011/06/18/2084406.html
C# KeyValuePair<TKey,TValue>的用法-转载的更多相关文章
- C# KeyValuePair<TKey,TValue>的用法
命名空间:System.Collections.Generic 构造函数:public KeyValuePair (TKey key, TValue value); 属性:只读属性 Key ,只读属性 ...
- C# KeyValuePair<TKey,TValue>与Container
KeyValuePair<TKey,TValue> KeyValuePair<TKey,TValue>是一个结构体,相当于C#一个Map类型的对象,可以通过它记录一个键/值对 ...
- List<KeyValuePair<TKey,TValue>> 与 Dictionary<TKey,TValue> 不同
两者都可以通过 KeyValuePair<TKey,TValue> 进行遍历,并且两者可以相互转换: List<KeyValuePair<string,string>&g ...
- C# KeyValuePair<TKey,TValue> 与 Dictionary<TKey,TValue> 区别
KeyValuePair<TKey,TValue> 可以设置.查询的一对键值 是struct Dictionary<TKey,TValue> 可以设置.查询的多对键值的集合 总 ...
- .net源码分析 – Dictionary<TKey, TValue>
接上篇:.net源码分析 – List<T> Dictionary<TKey, TValue>源码地址:https://github.com/dotnet/corefx/blo ...
- IDictionary<TKey, TValue> vs. IDictionary
Enumerating directly over an IDictionary<TKey,TValue>returns a sequence of KeyValuePair struc ...
- Dictionary<TKey, TValue> 类
C# Dictionary<TKey, TValue> 类 Dictionary<TKey, TValue> 泛型类提供了从一组键到一组值的映射.字典中的每个添加项都由一个值及 ...
- wp7 BaseDictionary<TKey, TValue>
/// <summary>/// Represents a dictionary mapping keys to values./// </summary>/// /// &l ...
- C# .Net 中字典Dictionary<TKey,TValue>泛型类 学习浅谈
一.综述: Dictionary<TKey,TValue>是在 .NET Framework 2.0 版中是新增的.表示键值对的集合,Dictionary<TKey,TValue&g ...
随机推荐
- canvas学习和面向对象(二)
Canvas 学习(二) 上一篇Canvas 学习(一)中我是用canvas绘制了一些基本和组合的图形. 现在开始绘制图片和动画帧,以及面向对象的升级版本. 还是一样,看代码,所有的代码都托管在git ...
- iOS homekit使用说明
本文由CocoaChina翻译组成员iBenjamin_Go和浅夏@旧时光翻译自苹果开发文档:HomeKit Developer Guide,敬请勘误. 本文档内容包括 第一部分:简介 第二部分:启用 ...
- 2.Java基础之Runtime对象
毕向东老师Java基础学习笔记——Runtime对象 今天学习Java中的Runtime对象后,感觉这个对象对我们主要有以下几点用处. 1.使用java代码打开本地可执行文件,比如打开一个计算器. 2 ...
- CSS魔法堂:重拾Border之——不仅仅是圆角
前言 当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...
- jQuery插件之ajaxFileUpload异步上传
介绍 AjaxFileUpload.js 是一个异步上传文件的jQuery插件,原理是创建隐藏的表单和iframe然后用JS去提交,获得返回值. 下载地址: http://files.cnblogs. ...
- 从零开始,搭建博客系统MVC5+EF6搭建框架(3),添加Nlog日志、缓存机制(MemoryCache、RedisCache)、创建控制器父类BaseController
一.回顾系统进度以及本章概要 目前博客系统已经数据库创建.以及依赖注入Autofac集成,接下来就是日志和缓存集成,这里日志用的是Nlog,其实还有其他的日志框架如log4,这些博客园都有很多介绍,这 ...
- 单例模式 - Singleton
对今天学习的Singleton Pattern简单总结下: 定义:保证一个类只有一个实例,必须自己创建自己的实例,并提供一个访问它的全局访问点. private 构造函数: private stati ...
- Java中随机数的产生方式与原理
查阅随机数相关资料,特做整理 首先说一下java中产生随机数的几种方式 在j2se中我们可以使用Math.random()方法来产生一个随机数,这个产生的随机数是0-1之间的一个double,我们可以 ...
- Socket编程——客户端,服务器端的读写操作
URL网络编程,最大的特征就是一对一的响应! 1:客户端“写”,服务器端用于“读” package coreBookSocket2; import java.io.InputStreamReader; ...
- Java动态代理全面分析
代理模式 解说:给某一个对象提供一个代理,并由代理对象控制对原对象的引用: 代理模式需要以下几个角色: 1 主题:规定代理类和真实对象共同对外暴露的接口: 2 代理类:专门代理真实对象的类: 3 ...