• notice
  • Congratulations on the launch of the Sought Tech site

The relationship between KeyValuePair and Dictionary

The relationship between KeyValuePair and Dictionary

1. KeyValuePair

a, KeyValuePair is a structure (struct);

b. KeyValuePair  only contains a key-value pair of Key and Value.

2. Dictionary

a. Dictionary can be simply regarded as  a collection of KeyValuePair ;

b. Dictionary  can contain multiple Key and Value key-value pairs.

The following code may help to better understand the relationship between them:

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

The usage of C# KeyValuePair<TKey,TValue>. The structure defines the key/value pairs that can be set or retrieved. In other words, we can record a value such as a key/value pair through it. For example, if we want to define a key/value pair like ID (int type) and Name (string type), then we can use it like this.

/// <summary>
/// Set key/value pairs
/// </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>
/// Get key/value pairs
/// </summary>
private void GetKeyValuePairDemo()
{
  KeyValuePair<int, string> kvp = SetKeyValuePair();
   int intKey = kvp.Key;
   string strValue = kvp.Value;
}

If you want to use generics, the same is true.Generally, when reading data in batches, when you only need to read two fields (Id and Name), if you don’t want to use the Model class, and use KeyValuePair with generics, for example:

1.Read data from the database

/// <summary>
/// Get the Id (enterprise_id) and English name (enterprise_name_eng) of all companies
/// </summary>
/// <returns>All the company IDs and English names in the enterprise_info table</returns>
public List<KeyValuePair<long,string>> GetEnterpriseIdAndNameEn 
gList()
{
  //enterprise_id key and enterprise_name_eng pair
  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.Processing data in business

/// <summary>
/// Function:
/// 1.Return the set of valid company Id obtained from the company name to be imported.
/// 2.Return a valid enterprise line number set.
/// 3.Return an invalid enterprise line number set.
/// </summary>
/// <param name=”lstEnterpriseNameEn”>The enterprise name (English) set to be imported</param>
/// <param name=”lstValidRowsIndex”>Valid enterprise Id row in Excel Set</param>
/// <param name=”lstInvalidRowsIndex”>Invalid enterprise Id row set in the Excel table</param>
/// <returns>returns the index list of valid rows</returns>
public List<long > PrepareForImport(List<string> lstEnterpriseNameEn, out List<int> lstValidRowsIndex, out List<int> lstInvalidRowsIndex)
{
  //Valid enterprise Id row
  lstValidRowsIndex = new List<int>();
  //Invalid enterprise Id row
  lstInvalidRowsIndex = new List<int>();    
  List<KeyValuePair<long, string>> lstIdKeyNameEngValue = dal.GetEnterpriseIdAndNameEn gList();     //It is used to store the Id of a valid enterprise, that is, if the English name of the enterprise can be found in the enterprise_info table, it means that the enterprise exists, so Get the existing enterprise Id and store it in this variable
  List<long> lstValidEnterpriseId = new List<long>();     //You can get a list of valid enterprise Ids through the following loop
   for (int i = 0; i <lstEnterpriseNameEn.Count; i++)
   {
      foreach (KeyValuePair<long, string> kvp in lstIdKeyNameEngValue)
      {
          if (lstEnterpriseNameEn[i] == kvp.Value)
          {
              //Get valid row index
              lstValidRowsIndex.Add(i);                 //Get valid enterprise Id
              lstValidEnterpriseId.Add(kvp.Key);                 //After finding a valid ID, immediately jump out of the inner loop and return to the outer loop
              continue;
          }



Tags

Technical otaku

Sought technology together

Related Topic

1 Comments

author

atorvastatin over the counter & lt;a href="https://lipiws.top/"& gt;buy lipitor without a preion& lt;/a& gt; lipitor for sale

Jfhgds

2024-03-09

Leave a Reply

+