Good, bad or just a bit ugly : string[] RemoveEmpties(string[] source)
I'm building up a bunch of conditions for an SqlWhereClause and there is an option to AND the various condition or to OR them.
I have a few function to build the conditions, but when I get around to putting them all together some of them are blank so when I String.Join them I get extra ANDs/ORs in my final statement.
This eventualy led me to the RemoveEmpties function, which takes a array of strings and returns an array that contains only the non-null non-empty ones.
private string JoinSQLFragments(string joiner,params string[] fragments)
{
return String.Join(joiner,RemoveEmpties(fragments));
}
private string[] RemoveEmpties(string[] source)
{
ArrayList list = new ArrayList(); // :(
foreach(string item in source)
{
if(item!=null && item.Length > 0)
{
list.Add(item);
}
}
return (string[]) list.ToArray(typeof(string));
}
I call these from something like the following, each of the BuildX methods can return a blank string.
JoinSQLFragments(" OR ",BuildMonthRangeCriteria("FieldA",criteriaA),BuildKeywordCriteria(keyword));
Any helpful thoughts or beauty tips ;) will be appreciated