如何使用EF4.1过滤可空的DateTime字段?

时间:2012-04-10 16:25:24

标签: asp.net entity-framework-4.1 nullable

我有一个可以为null的DateTime字段的表:

CREATE TABLE [dbo].[myTable](
  [ID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
  ...
  [FinishedDate] [datetime] NULL,
  etc...

当我尝试这个时:

var activeThings = from foo in _context.myTable
                   where foo.FinishedDate == null
                   select foo;

foreach ( var thing in activeThings ) {
   ... do some stuff ...
}

我没有得到任何价值。如何在空值上过滤?

1 个答案:

答案 0 :(得分:1)

var activeThings = from foo in _context.myTable
                   where !foo.FinishedDate.HasValue //foo.FinishedDate.HasValue==false
                   select foo;

Source

HasValue返回布尔值,

是true =>不为空

是假=>空