Is where a way to check if DateTime is null in linq expression? I've IEnumeable method where I'm returning data from database
return _requestRepository.ExecuteProcReader(
myRequest,
new SqlParameter("@userName", user)).Select(items => new Feed
{
Id = (int)items[0],
Title = items[1].ToString(),
Body = items[2].ToString(),
Link = items[3].ToString(),
PubDate = (DateTime) items[4]
});
And items[4] is a datetime which can be null in database. So, how can check something like
if(items[4] is DateTime)
{
PubDate = (DateTime) items[4]
}
Where()
before your select method like:Where(p => p.PubDate != null)
, but if yourDateTime
isn't nullable you'll get an exception anyway – Spluf Apr 20 '16 at 12:57PubDate
nullable, an alternative is this:PubDate = items[4] != null ? (DateTime)items[4] : DateTime.MinValue
– Alexander Derck Apr 20 '16 at 12:58DateTime
is value type and cannot be null unless expilictly defined a asDateTime?
. – Amit Kumar Ghosh Apr 20 '16 at 13:00