2012-09-08 33 views
0

什麼是我要檢查空值,所以當這個語句的執行我沒有得到一個錯誤的最好方法:您在LINQ聲明空值,因此犯規發生錯誤

if (Levels.Count(x => x.Location.ToUpper() == code.ToUpper()) == 1) 

我需要做肯定位置不爲空,因爲它一直拋出一個對象引用異常。

回答

1

您可以Where運營商嘗試

if (Levels.Where(x => x.Location != null) 
      .Count(x => x.Location.ToUpper() == code.ToUpper()) == 1) 
+0

謝謝,這是我需要什麼。 – mameesh

+0

我很樂意幫助你mameesh –

2

試試這個

if (Levels.Count(x => x.Location!= null && x.Location.ToUpper() == code.ToUpper()) == 1)