Andrew Stevenson's WebLog

Write Here Write Now

Syndication

.Net Languages

ADO.Net

Asp.Net (Misc.)

Asp.Net Controls

Bad Patterns

Err What?

Files and Folders

Forums

IE Add-Ins

Methods for the Madness

Performance

See Clearer, C#

Test Driven

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

Published Wednesday, July 28, 2004 9:19 AM by AndrewSeven

Comments

No Comments