I have this method that is supposed to get the latest messages posted, from the Table (& EntitySet) called ENTRY
///method gets "days" as parameter, used in 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 the DATECREATED is Nullable in the database. I cant place "if"s in this query ... any way to check for that? Thx in advance
What do you want to do in case DATECREATED
is null
?
If you just want to ignore these records use an additional condition(or where
clause):
var entries = from ent in db.ENTRY
where ent.DATECREATED.HasValue && ent.DATECREATED.Value > ...
Well... you can exclude entries with DATECREATED equal to null by simply using
entries = from ent in db.ENTRY
where ent.DATECREATED != null
where ent.DATECREATE.Value > DateTime.Today....
and include them with
entries = from ent in db.ENTRY
where ent.DATECREATED == null ||
ent.DATECREATE.Value > DateTime.Today.....
I am not sure though if the second query actually stops checking the where clauses once the first condition is true.
来源:https://stackoverflow.com/questions/2757472/linq-2-entities-howto-check-datetime-hasvalue-within-the-linq-query