【发布时间】:2013-05-19 15:36:46
【问题描述】:
我认为我的代码应该使ViewBag.test 属性等于"No Match",但它却抛出了InvalidOperationException。
这是为什么?
string str = "Hello1,Hello,Hello2";
string another = "Hello5";
string retVal = str.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
.First(p => p.Equals(another));
if (str == another)
{
ViewBag.test = "Match";
}
else
{
ViewBag.test = "No Match"; //this does not happen when it should
}
【问题讨论】:
-
@SLaks 代码返回 System.InvalidOperationException 而不是在页面上显示“No Match”。
-
它不会返回
InvalidOperationException,它会抛出它。主要是因为该列表中没有字符串等于“Hello5”。你想完成什么? -
.First() 抛出它,如果没有匹配,请尝试 FirstOrDefault() 并检查 null
标签: c# asp.net-mvc exception equality invalidoperationexception