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>的用法-转载的更多相关文章

  1. C# KeyValuePair<TKey,TValue>的用法

    命名空间:System.Collections.Generic 构造函数:public KeyValuePair (TKey key, TValue value); 属性:只读属性 Key ,只读属性 ...

  2. C# KeyValuePair<TKey,TValue>与Container

    KeyValuePair<TKey,TValue>  KeyValuePair<TKey,TValue>是一个结构体,相当于C#一个Map类型的对象,可以通过它记录一个键/值对 ...

  3. List<KeyValuePair<TKey,TValue>> 与 Dictionary<TKey,TValue> 不同

    两者都可以通过 KeyValuePair<TKey,TValue> 进行遍历,并且两者可以相互转换: List<KeyValuePair<string,string>&g ...

  4. C# KeyValuePair<TKey,TValue> 与 Dictionary<TKey,TValue> 区别

    KeyValuePair<TKey,TValue> 可以设置.查询的一对键值 是struct Dictionary<TKey,TValue> 可以设置.查询的多对键值的集合 总 ...

  5. .net源码分析 – Dictionary<TKey, TValue>

    接上篇:.net源码分析 – List<T> Dictionary<TKey, TValue>源码地址:https://github.com/dotnet/corefx/blo ...

  6. IDictionary<TKey, TValue> vs. IDictionary

    Enumerating directly over an IDictionary<TKey,TValue>returns a sequence of  KeyValuePair struc ...

  7. Dictionary<TKey, TValue> 类

    C# Dictionary<TKey, TValue> 类 Dictionary<TKey, TValue> 泛型类提供了从一组键到一组值的映射.字典中的每个添加项都由一个值及 ...

  8. wp7 BaseDictionary<TKey, TValue>

    /// <summary>/// Represents a dictionary mapping keys to values./// </summary>/// /// &l ...

  9. C# .Net 中字典Dictionary<TKey,TValue>泛型类 学习浅谈

    一.综述: Dictionary<TKey,TValue>是在 .NET Framework 2.0 版中是新增的.表示键值对的集合,Dictionary<TKey,TValue&g ...

随机推荐

  1. LeetCode - 404. Sum of Left Leaves

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  2. 7.Struts2复杂类型数据的接受

    复合类型数据的接收 所谓复合类型数据是指,一个JavaBean实例的属性值,而这些值又作为参数传递给Action. Action若要接收这些数据,需要做到以下几点: (1)定义Action时,将该Be ...

  3. js作用域和变量提升

    Function declarations and variable declarations are always moved (“hoisted”) invisibly to the top of ...

  4. 从零开始学习jQuery (三) 管理jQuery包装集

    本系列文章导航 从零开始学习jQuery (三) 管理jQuery包装集 一.摘要 在使用jQuery选择器获取到jQuery包装集后, 我们需要对其进行操作. 本章首先讲解如何动态的创建元素, 接着 ...

  5. 我对Backbone.js的一些认识

    backbone.js已经不是当前最流行的前端框架了,但是对于我而言,依然具有比较好的学习价值.虽然目前来说,react,vue等mvvm框架非常火热,但是感觉自身还不到去使用这种框架的层次.这些技术 ...

  6. Autofac - 程序集扫描

    通过程序集扫描, 能够自动注册符合规则的类型. 这种方式, 很方便. 这一篇就介绍下程序集扫描吧. 一.扫描 其实前面已经介绍过, 这种方式. 不过并不全. 先看一个之前的方式: var builde ...

  7. javascript设计模式实践之职责链--具有百叶窗切换图片效果的JQuery插件(三)

    在上一篇<javascript设计模式实践之模板方法--具有百叶窗切换图片效果的JQuery插件(二)>里,通过采用模板方法模式完成了切换效果对象的构建编写. 接下来就是完成各效果对象的调 ...

  8. .NET Task揭秘(一)

    Task为.NET提供了基于任务的异步模式,它不是线程,它运行在线程池的线程上.本着开源的精神, 本文以解读基于.NET4.5 Task源码的方式来揭秘Task的实现原理.   Task的创建 Tas ...

  9. 【C#进阶系列】22 CLR寄宿和AppDomain

    关于寄宿和AppDomain 微软开发CLR时,将它实现成包含在一个DLL中的COM服务器. 任何Windows应用程序都能寄宿(容纳)CLR.(简单来讲,就是CLR在一个DLL中,通过引用这个DLL ...

  10. iOS面试用到的一些知识点和技术

    1.APP的生命周期和viewcontroler的生命周期? 答:APP的生命周期:在APP的代理中分为七个阶段: 1.将要启动 2.已经启动 3.将要进入非活动状态 4.进入后台 5.从后台进入前台 ...