var feins1 = from business in Database
where business.Name == name
&& (business.City == city)
&& (business.State == state)
select business.FEIN;
if (feins1.Count() > 0) {
return feins1.First();
}
var feins2 = from business in Database
where business.Name == name
&& (business.City == city)
select business.FEIN;
if (feins2.Count() > 0) {
return feins2.First();
}
var feins3 = from business in Database
where business.Name == name
select business.FEIN;
if (feins3.Count() > 0) {
return feins3.First();
}
You reach for the ctrl-c and ctrl-v on your keyboard, and suddenly feel really dirty. What if another criteria needs to be added? You'll have to add another criteria to each where statement, as well as adding an entirely new query. I spent a few minutes thinking about it, and came up with this solution:
for (int i = 0; i < 3; i++) {
fein = GetBestFEIN("Good Times Inc", "Indianapolis", "IN", i > 1, i > 0);
if (fein != string.Empty) {
break;
}
}
return fein;
}
private string GetBestFEIN(string name, string city, string state, bool skipCity, bool skipState) {
var Database = from i in new string[] { "0", "1", "2", "3" }
select new { Name = i, FEIN = i, City = i, State = i };
var feins = from business in Database
where business.Name == name
&& (skipCity || business.City == city)
&& (skipState || business.State == state)
select business.FEIN;
if (feins.Count() == 0) {
return string.Empty;
} else {
return feins.First();
}
}
Now if another criteria is added, you just need to increase the for each loop by one, and add another parameter. I made my change, and went home feeling a lot cleaner.
No comments:
Post a Comment