
Untitled
By: a guest on
May 10th, 2012 | syntax:
None | size: 3.04 KB | hits: 13 | expires: Never
Creating Method in C#
foreach (KeyValuePair<int?, string> sourceVal in Source) {
try {
if (p.LeadOriginDetail != null) {
if (sourceVal.Value.ToUpper() == p.LeadOriginDetail.ToUpper()) {
sourceid = sourceVal.Key;
break;
} else {
sourceid = null;
}
} else {
sourceid = null;
}
} catch (Exception ex) {
CPCUtilities.LogGeneral("Error: " + ex.ToString());
}
}
int? Find(IEnumerable<KeyValuePair<int?, string>> items, string origin) {
int? resId = null;
foreach (KeyValuePair<int?, string> sourceVal in items) {
try {
if (origin != null) {
if (sourceVal.Value.ToUpper() == origin.ToUpper()) {
resId = sourceVal.Key;
break;
} else {
resId = null;
}
} else {
resId = null;
}
} catch (Exception ex) {
CPCUtilities.LogGeneral("Error: " + ex.ToString());
}
}
return resId;
}
var sourceId = Find(Source, p.LeadOriginDetail);
var marketId = Find(Market, p.LeadOriginDetail);
var businessUnitId = Find(BusinessUnit, p.LeadOriginDetail);
private static void YourSampleMethod()
{
foreach (KeyValuePair<int?, string> sourceVal in Source)
{
try
{
if (p.LeadOriginDetail != null)
{
if (sourceVal.Value.ToUpper() == p.LeadOriginDetail.ToUpper())
{
sourceid = sourceVal.Key; break;
}
else
{
sourceid = null;
}
}
else
{
sourceid = null;
}
}
catch (Exception ex)
{
CPCUtilities.LogGeneral("Error: " + ex.ToString());
}
}
}
int? FindKeyForValue(IEnumerable<KeyValuePair<int?,string> sequence,string valueToFind)
{
if(valueToFind!=null)
return sequence
.Where(string.Equals(pair.Value,valueToFind,StringComparison.CurrentCultureIgnoreCase))
.Select(pair=>pair.Value)
.FirstOrDefault();
else
return null;
}
int? FindKeyForValue(IEnumerable<KeyValuePair<int?,string> sequence,string valueToFind)
{
if(valueToFind!=null)
return sequence
.FirstOrDefault(string.Equals(pair.Value,valueToFind,StringComparison.CurrentCultureIgnoreCase))
.Key;
else
return null;
}
foreach([itemclass] Source in new [collectionclass][] { Source1, Source2, Source3, Source4... }) {
foreach (KeyValuePair<int?, string> sourceVal in Source) {
// everything else
}
}
IEnumerable<KeyValuePair<int?, string>>