2010-11-01 62 views
0

我有一個Employee類型的對象的數組,如何檢查在LINQ查詢可空日期

var s = from x in employee 
          where !string.IsNullOrEmpty(x.FirstName) && (x.FirstName.IndexOf(searchText, StringComparison.OrdinalIgnoreCase)) >= 0 || !string.IsNullOrEmpty(x.LastName) && (x.LastName.IndexOf(searchText, StringComparison.OrdinalIgnoreCase)) >= 0 || 

//I want to check here for null DateOfBirth 
x.DateOfBirth.Value.ToShortDateString().StartsWith(searchText, StringComparison.OrdinalIgnoreCase) 
          select x; 

回答

0

我認爲,所有你需要的是

&& x.DateOfBirth.HasValue && 

添加到您的where子句過濾掉空白的dobs。

我假設DateOfBirth是一個可爲空的DateTime。