Is there a simpler way to do this if statement in C#

后端 未结 7 1442
说谎
说谎 2021-01-21 03:05

I have the following:

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

I have more like t

7条回答
  •  爱一瞬间的悲伤
    2021-01-21 03:56

    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.

提交回复
热议问题