keyvaluepair_KeyValuePair用法(转)

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

///

/// 设置键/值对

///

///

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,示例:

1、从数据库中读取数据

///

/// 获取所有企业的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 ( 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、在业务中处理数据

///

/// 函数作用:

/// 1、返回从待导入的企业名称中获的有效企业Id集。

/// 2、返回有效的企业行号集。

/// 3、返回无效的企业行号集。

///

/// 待导入的企业名称(英文)集

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

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Twitter Digg Facebook Del.icio.us Reddit Stumbleupon Newsvine Technorati Mr. Wong Yahoo! Google Windows Live Send as Email Add to your CodeProject bookmarks Discuss this article 85 Print Article Database » Database » Other databasesLicence CPOL First Posted 19 Jan 2012 Views 24,219 Downloads 992 Bookmarked 74 times RaptorDB - The Key Value Store V2 By Mehdi Gholam | 8 Mar 2012 | Unedited contribution C#.NETDBABeginnerIntermediateAdvanceddatabase Even faster Key/Value store nosql embedded database engine utilizing the new MGIndex data structure with MurMur2 Hashing and WAH Bitmap indexes for duplicates. See Also More like this More by this author Article Browse Code Stats Revisions (8) Alternatives 4.95 (56 votes) 1 2 3 4 5 4.95/5 - 56 votes μ 4.95, σa 1.05 [?] Is your email address OK? You are signed up for our newsletters but your email address is either unconfirmed, or has not been reconfirmed in a long time. Please click here to have a confirmation email sent so we can confirm your email address and start sending you newsletters again. Alternatively, you can update your subscriptions. Add your own alternative version Introduction What is RaptorDB? Features Why another data structure? The problem with a b+tree Requirements of a good index structure The MGIndex Page Splits Interesting side effects of MGIndex The road not taken / the road taken and doubled back! Performance Tests Comparing B+tree and MGIndex Really big data sets! Index parameter tuning Performance Tests - v2.3 Using the Code Differences to v1 Using RaptorDBString and RaptorDBGuid Global parameters RaptorDB interface Non-clean shutdowns Removing Keys Unit tests File Formats File Format : *.mgdat File Format : *.mgbmp File Format : *.mgidx File Format : *.mgbmr , *.mgrec History Download RaptorDB_v2.0.zip - 38.7 KB Download RaptorDB_v2.1.zip - 39 KB Download RaptorDB_v2.2.zip - 39 KB Download RaptorDB_v2.3.zip - 39.6 KB D

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值