Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare only Date in nHibernate Linq on a DateTime value

I trying to compare two dates (DateTime) in nHibernate linq:

query = query.Where(l => (l.datCriacao.Date == dtLote.Date)

but I am getting the error:

NHibernate.QueryException: could not resolve property: datCriacao.Date of: SAGP.Entities.Lote

Anyone knows how I can solve this? Thanks

like image 227
Fernando Avatar asked Nov 12 '09 18:11

Fernando


People also ask

How to compare date in LINQ query?

Condition 1) StartDate is less than or equal to current date and enddate is Greater than current date. Condition 2) End date is less than today/current date. and if we have to get only count of rows not the dt row.

How to get only date in LINQ?

ToString("dd-mm-yyyy");


3 Answers

I solved the problem doing a between with the dates:

DateTime initialDate, finalDate;
initialDate= DateEntity.Date;
finalDate= new DateTime(DateEntity.Year, DateEntity.Month, DateEntity.Day, 23, 59, 59);
query = query.Where(l => (((l.dateEntity>= initialDate) && (l.dateEntity<= finalDate))
like image 90
Fernando Avatar answered Sep 28 '22 00:09

Fernando


This is super old, but I'd add to jaspion's example as:

query = query.Where(l => (l.datCriacao >= dtLote.Date && l.datCriacao < dtLote.Date.AddDays(1))
like image 28
Chris M Avatar answered Sep 28 '22 00:09

Chris M


You can check the condition like this

var nextDay = DateTime.Today.AddDays(1);

query = query.Where(l => (l.datCriacao >= dtLote && l.datCriacao < nextDay);

here you'll get the records on dtLote date as we checking between dtLote and dtLote+1 day (00:00:00) we'll get today's date record only what ever may be the time...

like image 33
Ejaz Avatar answered Sep 28 '22 01:09

Ejaz