开发者

How to Check for the nullable dates in the linq query

开发者 https://www.devze.com 2023-01-23 02:44 出处:网络
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)) >

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消