LINQ 2 Entities checking DateTime.HasValue in linq query

I have this method that should receive the last sent messages from a table (& EntitySet) named ENTRY

/// method gets "days" as the parameter used in the new TimeSpan (days, 0,0,0); !!

using (Entities db = new Entities())
    {
        var entries = from ent in db.ENTRY
                      where ent.DATECREATE.Value > DateTime.Today.Subtract(new TimeSpan(days, 0, 0, 0))
                      select new ForumEntryGridView()
                      {
                          id = ent.id,
                          baslik = ent.header,
                          tarih = ent.entrydate.Value,
                          membername = ent.Member.ToString()
                      };
        return entries.ToList<ForumEntryGridView>();
    }

Here DATECREATED is Nullable in the database. I can’t indicate if if in this query ... any way to check this? thanks in advance

+3
source share
2 answers

What do you want to do, if DATECREATEDany null?

If you just want to ignore these entries, use an additional condition (or where):

var entries = from ent in db.ENTRY
              where ent.DATECREATED.HasValue && ent.DATECREATED.Value > ...
+4
source

... DATECREATED ,

entries = from ent in db.ENTRY 
          where ent.DATECREATED != null
          where ent.DATECREATE.Value > DateTime.Today....

  entries = from ent in db.ENTRY 
          where ent.DATECREATED == null ||
                ent.DATECREATE.Value > DateTime.Today.....

, where, .

0

Source: https://habr.com/ru/post/1743800/


All Articles