I have about 9 of these just with different values for were “sourceVal” and “Source” is. I would like to make a method and just pass those two values for each one so my code is cleaner. Is there an easy way to make a method out of this?
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());
}
}
Thanks!
If you would like to refactor it manually, try this:
Now you can invoke this logic as follows:
There is an easier way of getting the first matching ID using LINQ – it is nearly always a better choice than rolling your own loop, let alone making nine similar loops.