keyvaluepair_c# KeyValuePair用法
private KeyValuePair SetKeyValuePair()
{
int intKey = 1;
string strValue = "My value";
KeyValuePair kvp = new KeyValuePair(intKey, strValue);
return kvp;
}
///
/// 获得键/值对
///
private void GetKeyValuePairDemo()
{
KeyValuePair kvp = SetKeyValuePair();
int intKey = kvp.Key;
string strValue = kvp.Value;
}
如果想使用泛型的话,一般批量读取数据的时候,当只需要读两个字段(Id and Name)时, 如果想不用Model类,并配合泛型使用KeyValuePair,示例:
///绑定一个cmbobox数据项
public void cmbDate{
List> dit = new List>();
if (Helper.GlobalVariables.Gi_Language_Type == 1)
{
dit.Add(new KeyValuePair(1, "身份证"));
dit.Add(new KeyValuePair(2, "军官证"));
dit.Add(new KeyValuePair(3, "护照"));
dit.Add(new KeyValuePair(4, "其他"));
}
else
{
dit.Add(new KeyValuePair(1, "Identity card"));
dit.Add(new KeyValuePair(2, "Officer certificate"));
dit.Add(new KeyValuePair(3, "Passport"));
dit.Add(new KeyValuePair(4, "Other"));
}
this.cmbCertificate_type.DataSource = dit;
this.cmbCertificate_type.DisplayMember = "Value";
this.cmbCertificate_type.ValueMember = "Key";
}
///
/// 获取所有企业的Id(enterprise_id)及英文名 (enterprise_name_eng)
///
/// enterprise_info表中的所有企业 Id及英文名
public List> GetEnterpriseIdAndNameEngList()
{
//enterprise_id键和enterprise_name_eng 对
List> lstIdKeyNameEngValue = new List>();
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 idKeyNameEngValue = new KeyValuePair (
&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、在业务中处理数据
/// 待导入的企业名称(英文)集
/// Excel表中有效的企业Id行集
/// Excel表中无效的企业Id行集
/// 返回有效的行的索引列表
public List PrepareForImport(List lstEnterpriseNameEn, out List lstValidRowsIndex, out List lstInvalidRowsIndex)
{
//有效的企业Id行
lstValidRowsIndex = new List();
//无效的企业Id行
lstInvalidRowsIndex = new List(); //获取所有的企业Id及英文名
List> lstIdKeyNameEngValue = dal.GetEnterpriseIdAndNameEngList(); //用于存放有效的企业的Id,即如果可以在enterprise_info表中找到此企业的英文名,那么表示此企业存在,因此把存在的企业Id获取出来,存放于此变量
List lstValidEnterpriseId = new List(); //通过以下循环可以获得可以有效的企业Id列表
for (int i = 0; i < lstEnterpriseNameEn.Count; i++)
{
foreach (KeyValuePair 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_c# KeyValuePair用法相关推荐
- keyvaluepair_【转】KeyValuePair用法
KeyValuePair用法(转) C# KeyValuePair的用法.结构体,定义可设置或检索的键/值对.也就是说我们可以通过它记录一个键/值对这样的值.比如我们想定义一个ID(int类型)和Na ...
- keyvaluepair_C# KeyValuePairTKey,TValue的用法【转】 .
KeyValuePair可以这样实例化 KeyValuePair mcmillan = new KeyValuePair("McMillan", 99); 取值 Console.W ...
- ajax异步后台存放购物车表,jQuery购物车插件jsorder用法(支持后台处理程序直接转换成DataTable处理)...
本文实例讲述了jQuery购物车插件jsorder用法.分享给大家供大家参考,具体如下: JSORDER 案例 案例一:我的菜单(点击菜名即可加入菜单) 红烧豆腐 12元 毛血旺 32元 套餐:京酱肉 ...
- C#中Dictionary的用法
在C#中,Dictionary提供快速的基于兼职的元素查找.他的结构是这样的:Dictionary<[key], [value]> ,当你有很多元素的时候可以使用它.它包含在System. ...
- keyvaluepair_KeyValuePair用法(转)
C# KeyValuePair的用法.结构体,定义可设置或检索的键/值对.也就是说我们可以通过 它记录一个键/值对这样的值.比如我们想定义一个ID(int类型)和Name(string类型)这样的键/ ...
- 关于Retrofit用法
Retrofit用法 一. 简介 随着Google对HttpClient 摒弃,和Volley的逐渐没落,OkHttp开始异军突起,而Retrofit则对okHttp进行了强制依赖. Retrofit ...
- c语言中external,static关键字用法
static用法: 在C中,static主要定义全局静态变量.定义局部静态变量.定义静态函数. 1.定义全局静态变量:在全局变量前面加上关键字static,该全局变量变成了全局静态变量.全局静态变量有 ...
- Pandas_transform的用法
先来看一个实例问题. 如下销售数据中展现了三笔订单,每笔订单买了多种商品,求每种商品销售额占该笔订单总金额的比例.例如第一条数据的最终结果为:235.83 / (235.83+232.32+107.9 ...
- Python中yield和yield from的用法
yield 后面接的是 future 对象 调用方 委托生成器 yield from 直接给出循环后的结果 yield from 委托者和子生成器直接通信 yield from 直接处理stopIte ...
最新文章
- php 请求方式,PHP发起HTTP请求有哪几种方式?
- iptables踩坑记
- 成功解决TypeError: ‘float’ object cannot be interpreted as an index
- k8s部署nacos集群:3个pod、连接外部mysql读写分离集群
- boost::property_tree模块自定义 ptree 的 data_type 需要执行的操作
- prometheus命令_Prometheus 入门
- gen文件下有两个R.java_android工程gen目录中R.java包名是怎么确定
- 【语义分割】论文代码资料汇总
- 微软彻底拥抱 Python!
- 如何计算机械能的变化量,问:如何计算机械能变化量?
- 【最短路径问题笔记】Floyd算法求多源最短路径问题
- 禁用Chrome自动更新
- 十大物流仿真软件汇总
- Rsyslog日志格式实例:记录IP地址而非主机名
- 嵌入式软件工程师是前端还是后端_【一线】当嵌入式软件工程师有什么感受
- 【HEVC】CTU,CU,PU,TU的划分
- 到位App_jQuery_art-template
- VC:CString.Format()函数详解
- 互联网行业的一些英文简称(缩写),别人家一说你又懵了!
- 超详细的WMS仓储管理系统介绍——补货篇