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 ...
随机推荐
- EntityFramework 7 OrderBy Skip Take-计算排序分页 SQL 翻译
先解释一下这个标题的意思,OrderBy 在 Linq 语句中,我们经常使用,比如 OrderBy(b => b.BlogId) 就是对 BlogId 字段进行升序排序,这是针对一个字段的排序, ...
- 使用纯前端JavaScript 实现Excel IO
公司最近要为某国企做一个**统计和管理系统, 具体要求包含 Excel导入导出 根据导入的数据进行展示报表 图表展示(包括柱状图,折线图,饼图),而且还要求要有动画效果,扁平化风格 Excel导出,并 ...
- 让你的JS更优雅的小技巧
首先,看一个非常不优雅的例子: 看到这段代码,虽然代码很短,但是一眼看上去就不想再看了,也就是没什么可读性.这段代码,没有封装,随意定义一个变量都是全局变量,这样在多人开发或者是大型开发中,极其容易造 ...
- ZOJ Problem Set - 1115 Digital Roots
水题记录: 注:此题题目并没有限定数值的大小,所以要用字符串进行处理 #include <stdio.h> #include <string.h> int main() { ] ...
- my SQL下载安装,环境配置,以及密码忘记的解决,以及navicat for mysql下载,安装,测试连接
一.下载 在百度上搜索"mysql-5.6.24-winx64下载" 二.安装 选择安装路径,我的路径“C:\Soft\mysql-5.6.24-winx64” 三.环境配置 计算 ...
- C++ 面试 (1) 指针
指针是C++中一类颇具特色的数据类型,允许直接操作内存地址,实现内存的动态分配.指针问题通常包括指针常量,常量指针,数组指针,指针数组,函数指针,指针传值等. 指针和引用的区别 非空区别.在任何情况下 ...
- 纯C#实现屏幕指定区域截屏
以前在别的地方见过一个通过调用系统API实现屏幕截图的例子,从内心来说我不太喜欢在C#代码中出现这种情况,现在什么都讲“和谐”,我觉得这种做法就是破坏了我们的“和谐”代码,呵呵,开玩笑,有的时候,不通 ...
- 高效的SQLSERVER分页查询的几种示例分析
Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询 ...
- 自定义分页控件PageList
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...
- Suggestion(搜索建议)产品和技术
今天来简单聊聊Suggestion产品 什么是Suggestion服务? 一图胜千言: 当你想要搜索某个长词语或者一句话输入部分时,Suggestion服务预测你极大可能的候选项,并罗列出来,供你选择 ...