转载自:http://blog.sina.com.cn/s/blog_9741eba801016w61.html

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;
}

KeyValuePair用法(转)的更多相关文章

  1. KeyValuePair

    KeyValuePair用法(转)(2012-06-25 10:47:35) 转载▼ // 标签: keyvaluepair it   KeyValuePair C# KeyValuePair< ...

  2. C# KeyValuePair<TKey,TValue>的用法-转载

    C# KeyValuePair<TKey,TValue>的用法.结构体,定义可设置或检索的键/值对.也就是说我们可以通过 它记录一个键/值对这样的值.比如我们想定义一个ID(int类型)和 ...

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

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

  4. Solr学习总结(六)SolrNet的高级用法(复杂查询,分页,高亮,Facet查询)

    上一篇,讲到了SolrNet的基本用法及CURD,这个算是SolrNet 的入门知识介绍吧,昨天写完之后,有朋友评论说,这些感觉都被写烂了.没错,这些基本的用法,在网上百度,资料肯定一大堆,有一些写的 ...

  5. System.Collections.Generic的各容器类的用法

    演示System.Collections.Generic的各容器类的用法. 包括:Dictionary,KeyValuePair,SortedDic tionary,SortedList,HashSe ...

  6. C#中Dictionary的用法及用途

    Dictionary<string, string>是一个泛型 他本身有集合的功能有时候可以把它看成数组 他的结构是这样的:Dictionary<[key], [value]> ...

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

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

  8. C#中Dictionary的用法

    在C#中,Dictionary提供快速的基于兼职的元素查找.他的结构是这样的:Dictionary<[key], [value]> ,当你有很多元素的时候可以使用它.它包含在System. ...

  9. Java Web EL JSTL的用法

    1.导入包 fastjson-1.2.2.jar 2.JSP文件加入 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" p ...

随机推荐

  1. Flex4 设置combobox选项不可编辑

    近日做数据的增删改查,使用的flex4的ComboBox控件---> flex4中ComboBox其实就是TextInput的叠加 flex3中ComboBox其实就是Label的叠加 开始是使 ...

  2. 发一个讨论帖,如果结果被采纳的话可以给一份adb 代码,以及我封装的ADBLIB

    如何在手机没有root 的情况下,获取系统的一些文件,比如 /data/data/xxxx 目录下的文件. 有任何想法的请说出来.

  3. Minimum Inversion Number(线段树求逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  4. Android 中文API (66) —— BluetoothClass.Device

    前言 本章内容是android.bluetooth.BluetoothClass.Device,为Android蓝牙部分的章节翻译,版本为Android 2.3   r1,翻译来自中山大学的" ...

  5. Spring技术内幕:Spring AOP的实现原理(一)

    一.SpringAOP的概述 1.AOP概念 AOP是Aspect-Oriented Programming(面向切面编程)的简称.维基百科的解释例如以下: Aspect是一种新的模块化机制,用来描写 ...

  6. java中只能有一个实例的类的创建

    Java中,如果我们创建一个类,想让这个类只有一个对象,那么我们可以 1:把该类的构造方法设计为private 2:在该类中定义一个static方法,在该方法中创建对象 package test; / ...

  7. jQuery+Ajax+Jsp做二级级联

    终于弄懂了这个级联,我去!必须得在博客记下来. 1, JS代码: $(document).ready(function(){ $("#select1").change(functi ...

  8. linux kernel中timer的使用

    linux kernel中timer的使用 http://blog.csdn.net/njuitjf/article/details/16888821 在kernel中如果想周期性的干些什么事情,或者 ...

  9. tasklet和工作队列

    tasklet机制和工作队列 http://blog.chinaunix.net/uid-28236237-id-3450753.html tasklet原理 http://www.kuqin.com ...

  10. poj 1726

    http://poj.org/problem?id=1276 解题要点:用完全背包来模拟的解题,只不过加了限制条件used[]...其他的就一样了.. 注意: cash 和n 为0 的情况 #incl ...