chris
chris

Reputation: 37480

How can I filter on a nullable DateTime field using EF4.1?

I have a table with a nullable DateTime field:

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

When I try this:

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

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

I get no values back. How can I filter this on null values?

Upvotes: 0

Views: 293

Answers (1)

B. Bilgin
B. Bilgin

Reputation: 783

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

Source

HasValue return boolean,

is true => not null

is false => null

Upvotes: 1

Related Questions