How to Check for the nullable dates in the linq query
I have a array of an object of type employee,
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, StringC开发者_如何转开发omparison.OrdinalIgnoreCase)
select x;
I think all you need is
&& x.DateOfBirth.HasValue &&
Added into your where clause to filter out dobs that are null.
I'm assuming DateOfBirth is a nullable DateTime.
精彩评论