小编use*_*766的帖子

为什么这段代码会抛出InvalidOperationException?

我认为我的代码应该使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
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc equality exception invalidoperationexception

8
推荐指数
1
解决办法
6万
查看次数