I am using Lucene in an application. As such I have a form that lets users build a query by selecting what they want to search from dropdowns. Once a user submits, I build the query and it comes down to something like this:
var formedQuery= string.Empty;
foreach(var field in fields)
{
if (field.name != 'condition so you never know which field from fields will be 1st')
formedQuery += " AND" + field.name + ":" field.value;
}
Now the problem with this is that the statement will begin with ' AND'
Now I usually finish with:
formedQuery = formedQuery.Substring(4) //Trim the first 4 characters
Would fellow programmers usually prefer to do:
var formedQuery= string.Empty;
var i = false;
foreach(var field in fields)
{
if (false &&
field.name != 'condition so you never know which field from fields will be 1st')
{
formedQuery += " AND" + field.name + ":" field.value;
i = true;
}
else
formedQuery += " " + field.name + ":" field.value;
}
Is there another technique people like to use for this sort of thing I am not thinking of? I prefer the former.