Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在linq表达式中检查DateTime是否为null_C#_Linq_Datetime - Fatal编程技术网

C# 在linq表达式中检查DateTime是否为null

C# 在linq表达式中检查DateTime是否为null,c#,linq,datetime,C#,Linq,Datetime,在linq表达式中,哪里可以检查DateTime是否为null?我有一个从数据库返回数据的IEnumable方法 return _requestRepository.ExecuteProcReader( myRequest, new SqlParameter("@userName", user)).Select(items => new Feed { Id = (int)items[0], Title = items[1].ToString(), Body =

在linq表达式中,哪里可以检查DateTime是否为null?我有一个从数据库返回数据的IEnumable方法

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]
});
而items[4]是一个datetime,在数据库中可以为null。那么,如何检查类似的内容呢

if(items[4] is DateTime)
{
   PubDate = (DateTime) items[4]
}

也许你可以在这里使用三元运算符

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]).HasValue ? (DateTime) items[4] : DateTime.Now
//Or any date you want to use
});

还有一个选项是在类
Feed
声明中将
PubDate
声明为可空

像这样:

class Feed {
  public DateTime? PubDate {get;set;}
  ...
}
这将把数据库中的真相暴露到数据访问层,并将空检查向上移动一级


请参阅:

从数据库获取数据时,还应检查DBNull.Value。 下面是我要做的:

PubDate=(项[4]==null | |项[4]==DBNull.Value?DateTime.Now:(DateTime)项[4])

如果数据库中有多个字段可以为NULL,则可以将其放在扩展方法中,如下所示:

public static object GetDBValue(this object value, object defaultValue)
{
  return value == null || value == DBNull.Value ? defaultValue : value;
}
并称之为:

PubDate = (DateTime)date1.GetDBValue(DateTime.Now);

在您选择的方法之前添加一个
Where()
,例如:
Where(p=>p.PubDate!=null)
,但是如果您的
DateTime
不可为空,您将得到一个异常,您应该真正使
PubDate
可为空,另一个选择是:
PubDate=items[4]!=无效的(DateTime)项[4]:DateTime.MinValue
@GrantWiney它是“DateTime”
DateTime
是值类型,除非明确定义为
DateTime?
DateTime没有名为
HasValue的成员,否则不能为空。这不会编译。我假设这里使用的是null datetime(datetime?)。因此它可能有HasValue成员。如果显式强制转换为不可为null的类型,那么静态类型是什么并不重要。试着在IDE中输入这个,它不会编译。