KeyValuePair 和 Dictionary 的关系

KeyValuePair 和 Dictionary 的关系

1、KeyValuePair

a、KeyValuePair 是一个结构体(struct);

b、KeyValuePair 只包含一个Key、Value的键值对。

2、Dictionary

a、Dictionary 可以简单的看作是KeyValuePair 的集合;

b、Dictionary 可以包含多个Key、Value的键值对。

下面的代码可能会帮助更好的理解它们之间的关系:

Dictionary<int, string> myDic = new Dictionary<int, string>();
foreach (KeyValuePair<int, string> item in myDic)
{
    Console.WriteLine("Key = {0}, Value = {1}", item.Key, item.Value);
}

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