0
我有一个SQL数据库建模这个类。LINQ中检查可能的空值
Public Class Language
Public Property Name As String
Public Property Articles As List(Of Article)
End Class
Public Class Article
Public Property Dated as DateTime?
End Class
我想语言的列表,也是最后一篇文章
的日期,但我要检查是否有物品的物品,如果物品有一个有效日期。
查询的正确方法是什么?我到目前为止已经做到了这一点:
Dim query = From x In context.Languages
Order By x.Name
Select New MyViewModel() With {
.Name = x.Name,
.LastArticleDate = If(x.Articles.Count <> 0 AndAlso
x.Articles.OrderByDescending(Function(x) x.Dated).FirstOrDefault IsNot Nothing AndAlso
x.Articles.OrderByDescending(Function(x) x.Dated).FirstOrDefault.Dated.HasValue,
x.Articles.OrderByDescending(Function(x) x.Dated).FirstOrDefault.Dated.Value.ToShortDateString,
String.Empty)
}
为什么你在不可空的'DateTime'属性上检查'HasValue'属性? 'Dated'确实是'DateTime?'? – MarcinJuraszek
是的,DateTime可以为空 – InfoStatus