处女问,期待中: Asp.Net里有没有类似sql中in函数的方法?
[前提]
定义2个字符串变量:
string strMonth = "";
string strResult = "";
[目的]
根据strMonth的值,设置strResult的值。
如果strMonth为"01"、"02"、"03"、"04"、"05"、"06"的话,则strResult="上半年";
如果strMonth为"07"、"08"、"09"、"10"、"11"、"12"的话,则strResult="下半年";
[我知道的比较笨的实现方式]
方式一:
if( (strMonth == "01") || (strMonth == "02") || (strMonth == "03") || (strMonth == "04") || (strMonth == "05") || (strMonth == "06") )
{
strResult="上半年"
}
if( (strMonth == "07") || (strMonth == "08") || (strMonth == "09") || (strMonth == "10") || (strMonth == "11") || (strMonth == "12") )
{
strResult="下半年"
}
方式二:
switch (strMonth)
{
case "01":
strResult = "上半年";
break;
case "02":
strResult = "上半年";
break;
case "03":
strResult = "上半年";
break;
case "04":
strResult = "上半年";
break;
case "051":
strResult = "上半年";
break;
case "06":
strResult = "上半年";
break;
case "07":
strResult = "下半年";
break;
case "08":
strResult = "下半年";
break;
case "09":
strResult = "下半年";
break;
case "10":
strResult = "下半年";
break;
case "11":
strResult = "下半年";
break;
case "12":
strResult = "下半年";
break;
default:
strResult = "未知";
break;
}
[问题]
?1: 有没有别的或者更好的实现方式? (实在不想写这么冗长的代码啊)
?2: asp.net中有没有类似sql文中的in一样的匹配(或者说是组合、判断)函数(方法)?比如上面的例子如果写成下面这样能实现的话该多好?
if ( strMonth in ("01","02","03","04","05","06")
{
strResult = "上半年";
}
if ( strMonth in ("07","08","09","10","11","12")
{
strResult = "上半年";
}
P.S: 这个问题困扰了我好久,初次发问,不好意思,还望高手或牛人指点一下,我顺便学学怎么送分,呵呵,谢谢了。
[解决办法]
转换成int型 <=6 "上半年“
>6 "下半年“
[解决办法]
arraylist .add("01").add("02").add()....
if(arraylist.contain(months)
{
上半年;
}
呵呵 这不就是in吗,
[解决办法]
正则Regex
01|02|03|04|05|06
07|08|09|10|11|12
[解决办法]
string strMonth = "05"; --如果strMonth非法,则下面语句会抛异常 string strResult =(DateTime.Parse("2008-"+strMonth+"-01").Month > 6)?"下半年":"上半年"; Console.WriteLine(strResult);/*上半年请按任意键继续. . .*/