Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 10th, 2012  |  syntax: None  |  size: 3.04 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Creating Method in C#
  2. foreach (KeyValuePair<int?, string> sourceVal in Source) {
  3.             try {
  4.                 if (p.LeadOriginDetail != null) {
  5.                     if (sourceVal.Value.ToUpper() == p.LeadOriginDetail.ToUpper()) {
  6.                         sourceid = sourceVal.Key;
  7.                         break;
  8.                     } else {
  9.                         sourceid = null;
  10.                     }
  11.                 } else {
  12.                     sourceid = null;
  13.                 }
  14.             } catch (Exception ex) {
  15.                 CPCUtilities.LogGeneral("Error: " + ex.ToString());
  16.             }
  17.         }
  18.        
  19. int? Find(IEnumerable<KeyValuePair<int?, string>> items, string origin) {
  20.     int? resId = null;
  21.     foreach (KeyValuePair<int?, string> sourceVal in items) {
  22.         try {
  23.             if (origin != null) {
  24.                 if (sourceVal.Value.ToUpper() == origin.ToUpper()) {
  25.                     resId = sourceVal.Key;
  26.                     break;
  27.                 } else {
  28.                     resId = null;
  29.                 }
  30.             } else {
  31.                 resId = null;
  32.             }
  33.         } catch (Exception ex) {
  34.             CPCUtilities.LogGeneral("Error: " + ex.ToString());
  35.         }
  36.     }
  37.     return resId;
  38. }
  39.        
  40. var sourceId = Find(Source, p.LeadOriginDetail);
  41. var marketId = Find(Market, p.LeadOriginDetail);
  42. var businessUnitId = Find(BusinessUnit, p.LeadOriginDetail);
  43.        
  44. private static void YourSampleMethod()
  45.     {
  46.         foreach (KeyValuePair<int?, string> sourceVal in Source)
  47.         {
  48.             try
  49.             {
  50.                 if (p.LeadOriginDetail != null)
  51.                 {
  52.                     if (sourceVal.Value.ToUpper() == p.LeadOriginDetail.ToUpper())
  53.                     {
  54.                         sourceid = sourceVal.Key; break;
  55.                     }
  56.                     else
  57.                     {
  58.                         sourceid = null;
  59.                     }
  60.                 }
  61.                 else
  62.                 {
  63.                     sourceid = null;
  64.                 }
  65.             }
  66.             catch (Exception ex)
  67.             {
  68.                 CPCUtilities.LogGeneral("Error: " + ex.ToString());
  69.             }
  70.          }
  71.     }
  72.        
  73. int? FindKeyForValue(IEnumerable<KeyValuePair<int?,string> sequence,string valueToFind)
  74. {
  75.     if(valueToFind!=null)
  76.         return sequence
  77.             .Where(string.Equals(pair.Value,valueToFind,StringComparison.CurrentCultureIgnoreCase))
  78.             .Select(pair=>pair.Value)
  79.             .FirstOrDefault();
  80.     else
  81.         return null;
  82. }
  83.        
  84. int? FindKeyForValue(IEnumerable<KeyValuePair<int?,string> sequence,string valueToFind)
  85. {
  86.     if(valueToFind!=null)
  87.         return sequence
  88.             .FirstOrDefault(string.Equals(pair.Value,valueToFind,StringComparison.CurrentCultureIgnoreCase))
  89.             .Key;
  90.     else
  91.         return null;
  92. }
  93.        
  94. foreach([itemclass] Source in new [collectionclass][] { Source1, Source2, Source3, Source4... }) {
  95.     foreach (KeyValuePair<int?, string> sourceVal in Source) {
  96.         // everything else
  97.     }
  98. }
  99.        
  100. IEnumerable<KeyValuePair<int?, string>>