我認爲我的代碼應該使ViewBag.test
屬性等於"No Match"
,而是它拋出一個InvalidOperationException
。這段代碼爲什麼會拋出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代碼返回,而不是在頁面上diplay 「不匹配」 System.InvalidOperationException。 – user2398766
它不** **回報'InvalidOperationException',它把它扔了。主要是因爲該列表中沒有等於「Hello5」的字符串。你想完成什麼? –
。首先()拋出它,如果沒有匹配,嘗試FirstOrDefault(),並檢查空 –