Given the following two cases, which one is preferable (If they’re both bad, doing it a completely different way is an option too)?
Convert.ToInt32 called in Where:
var items = GetItems();
if (aDropDownList.SelectedIndex > 0)
{
items = items.Where(x =>
x.IntProperty == Convert.ToInt32(aDropDownList.SelectedValue));
}
Convert.ToInt32 called before Where:
var items = GetItems();
if (aDropDownList.SelectedIndex > 0)
{
int selectedDropDownValue = Convert.ToInt32(aDropDownList.SelectedValue);
items = items.Where(x => x.IntProperty == selectedDropDownValue);
}
I would prefer the second. It does only one conversion, instead of many.
But unless this is a performance-critical piece of code (unlikely; it seems like GUI code), you won’t notice a difference.