英文:
How can I reduce the no of IFs
问题
如何减少IF语句的数量并有效地重写相同的内容。
名称是动态的。名称可以是"一百"或"一千"。它也可以是"二百"或"二千"。我希望如果名称包含单词'one'或'two',它会进入相应的IF条件。
ID和名称包含相同的字符串。ID也将包含"一百"或"一千"...
var numberWords = new List<string> { "One", "Two", "Three" }; // Add more numbers as needed
foreach (var numberWord in numberWords)
{
if (Name.Contains(numberWord))
{
var checkIndex = Chart.YAxis.FindIndex(f => f.Index == index && f.Id.Contains(numberWord));
axisIndex = AddAxis(checkIndex, axis);
break; // Exit the loop once a match is found
}
}
// Handle the case when there is no match
if (axisIndex == -1)
{
// ...
}
谢谢。
英文:
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.
答案1
得分: 3
你可以使用这样的方法:
var possibleNames = new[] { "One", "Two", "Three", "Four" };
var selectedName = possibleNames.FirstOrDefault(n => Name.Contains(n));
if (string.IsNullOrWhiteSpace(selectedName)) // 返回或抛出异常
var index = Chart.YAxis.FindIndex(f => f.Index == index && f.Id.Contains(selectedName));
axisIndex = AddAxis(index, axis)
英文:
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)
</details>
# 答案2
**得分**: 0
```python
我会尝试解析“Name”字段以获取字符串值。然后剩下的就应该很容易了。
var value Name.Split(' ')[0];
var index = Chart.YAxis.FindIndex(f => f.Index == index && f.Id.Contains(value));
axisIndex = AddAxis(index , axis);
如果[String.Split][1]无法胜任,[正则表达式][2]也许能够获得你想要的值。
[1]: https://learn.microsoft.com/en-us/dotnet/api/system.string.split?view=net-7.0
[2]: https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference
英文:
I'd try to parse the Name field to get the string value. Then the rest should be easy.
var value Name.Split(' ')[0];
var index = Chart.YAxis.FindIndex(f => f.Index == index && f.Id.Contains(value));
axisIndex = AddAxis(index , axis);
If String.Split isn't up to it a regular expression may be able to get the value you want.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论