How can I reduce the no of IF statements and rewrite the same efficiently.
Name is dynamic. Name could be "One hundred" or "one thousand". It can "Two hundred" or "Two thousand". I want it to enter the respective IF if Name contains the word ‘one’ or ‘two’.
ID and Name contain same string. ID will also hold "One hundred" or "one thousand"…
if (Name.Contains("One"))
{
var checkIndex = Chart.YAxis.FindIndex(f => f.Index == index && f.Id.Contains("One"));
axisIndex = AddAxis(checkIndex, axis);
}
else if (Name.Contains("Two"))
{
var checkIndex = Chart.YAxis.FindIndex(f => f.Index == index && f.Id.Contains("Two"));
axisIndex = AddAxis(checkAxisIndex, axis);
}
else if (Name.Contains("Three"))
{
var checkIndex = Chart.YAxis.FindIndex(f => f.Index == index && f.Id.Contains("Three"));
axisIndex = AddAxis(checkAxisIndex, axis);
}
.....
else
{
....
}
Thank you.
>Solution :
You can use an approach like this :
var possibleNames = new []{"One", "Two", "Three", "Four"};
var selectedName = possibleNames.FirstOrDefault(n=> Name.Contains(n));
if(string.IsNullOrWhiteSpace(selectedName)) // return or throw
var index = Chart.YAxis.FindIndex(f => f.Index == index && f.Id.Contains(selectedName));
axisIndex = AddAxis(index , axis)