Is there a simpler way to do this if statement in

2020-04-10 23:04发布

I have the following:

if (model.PartitionKey.Substring(2, 2) == "05" || 
    model.PartitionKey.Substring(2, 2) == "06")

I have more like this. Is there a more clean way to code this where I don't have to repeat model.PartitionKey twice ?

标签: c#
7条回答
Melony?
2楼-- · 2020-04-10 23:35

You can save the substring in a variable:

var substring = model.PartitionKey.Substring(2, 2);
if (substring == "05" || substring == "06")

Or you could use a regular expression:

if (Regex.IsMatch("^..0[56]", model.PartitionKey))

This probably depends a bit on how easily you can understand a regex while reading code.

查看更多
登录 后发表回答