Compare only Date in nHibernate Linq on a DateTime value - linq

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

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))

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))

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...

Related

Compare datetime column just year (without time-day-month) with Linq

I want to get a 2022 records. But my code is not efficient. How to compare it?
> var _year = "2022"; list= query.Where(x =>
> Convert.ToDateTime(x.created_time).ToString("YYYY").Equals(_year
> )).ToList();
Is better not to change type to string, but get DateTime structure Year property.
Code example:
var year = 2022;
var list = query.Where(x => Convert.ToDateTime(x.created_time).Year.Equals(year)).ToList();

How to compare a date in ruby?

I am running a SQL query which returns some orders each having a date. I want to check which orders were made on current date ?
I am able to do so by:
orders = Order.find_by_sql(query).reject {|o|
today = Time.now
end_of_today = Time.local(today.year, today.month, today.day, 23, 59, 59)
start_of_today = Time.local(today.year, today.month, today.day, 00,00,00 )
o.date > end_of_today
o.date < start_of_today
}.sort_by {|o|
o.delivery_date
}
'orders' contain all the orders which were made at any time Today.
Is there a simpler way of doing this in ruby ?
To get the orders made on the current date, you can do the following:
orders = Order.where("date >= ?", Time.zone.now.beginning_of_day).order(:delivery_date)
Hope this helps!
use DateTime.now.beginning_of_day
Order.where('date >= ?', DateTime.now.beginning_of_day).order(:delivery_date)
I think you are looking for this, it will cover all orders for today
orders = Order.where("created_at <= ? AND created_at >= ?", Time.zone.now.end_of_day, Time.zone.now.beginning_of_day).order(:delivery_date)
Now you are getting all the orders from the database (depends on what string is in the query) and then filtering the array, which is not the most efficient way if you have a large database. Much better way is doing this on SQL side.
Just use pure string conditions:
# Or set it to any other date in the past
target_date = Time.zone.now
orders = Order.find_by_sql(query)
.where("date >= ?", target_date.beginning_of_day)
.where("date <= ?", target_date.end_of_day)
.order(:delivery_date)
More details here: http://guides.rubyonrails.org/active_record_querying.html#pure-string-conditions
More info on Active Support DateTime methods: http://api.rubyonrails.org/classes/DateTime.html#method-i-beginning_of_day

Entity framework and datetime compare

Is possible to create linq query to find all records, which are in date range?
For example :
I have table with VacationStart, VacationEnd - both datetime and i need find all pending vacations to current date.
I am trying
context.Vacations..Where(x=> x.VacationStart >= DateTime.Now && x.VacationEnd < DateTime.Now)
but i getting 0 records...
Sample data 2011-10-27 08:30:00.000 2011-10-28 17:00:00.000
Current date : 2011-10-26 23:39:46.297
Where i do mistake?
Thanks
You are asking for vacations that start after NOW and end before NOW, Impossible. try asking and comparing a start time and end time that are different and where start time is less than the end time.
Gives only the vacations currently in progress.
var query = context.Vacations.Where(v => v.VacationEnd > DateTime.Now
&& v.VacationStart < DateTime.Now);
Gives vacations that haven not begun yet.
var query = context.Vacations.Where(v => v.VacationStart > DateTime.Now);
Gives Vacations that are in progress or still have not started.
var query = context.Vacations.Where(v => v.VacationEnd > DateTime.Now);
The last one doesn't filter any future vacations.

Filtering by Date (linq)

I have DateTime field named "repeat" in database. I want to take only records which Date in "repeat" is today.
I tried :
(...).Where(e => e.repeat.Value.Date.Day.Equals(DateTime.Now.Day));
or:
(...).Where(e => e.repeat.Value.Date.Day==DateTime.Now.Day);
but it doesn't work.
Any ideas?
Thanks,Kamil
"It doesn't work" is pretty vague, but currently you're checking the day of the week rather than the date. I would suggest using:
Date today = DateTime.Today;
var query = ....Where(e => e.repeat.Value.Date == today);
EDIT: It's not clear what the types involved here are - if repeat.Value is not itself a DateTime, you may want:
var query = ....Where(e => e.repeat.Value.Date.Date == today);
Give this a try:
(...).Where(e => (e.repeat >= DateTime.Today) && (e.repeat < DateTime.Today.AddDays(1)))
Compare short date ensures you the year/month/day are the same in one shot :
(...).Where(e => e.repeat.Value.Date.ToShortDateString() == DateTime.Now.ToShortDateString());
Expert Solution is: Use DbFunctions.CreateDateTime() in Linq Query.
var date = DateTime.Now.AddDays(-1).Date;
var actions = await _context.Set<Your_table>()
.AsNoTracking()
.Where(x => DbFunctions.CreateDateTime(x.YourColumn.Value.Year, x.YourColumn.Value.Month, x.YourColumn.Value.Day, 0, 0, 0) == date)
.ToListAsync();

BETWEEN EQUIVALENT in LINQ

I am looking LINQ equivalent for the following query
Select * from ct_rate
WHERE
'2010-10-01 00:00:00'
BETWEEN start_date and end_date;
ANY IDEA?
You need to use two comparison operations:
DateTime date = new DateTime(2010, 10, 1);
var results = from rate in ct_rates
where rate.StartDate <= date && rate.EndDate >= date
select rate;
Just use ordinary comparison operators
var result = ct_rates
.Where(x => x.start_date <= myDate && x => x.endDate >= myDate);
In our case, Robert's answer was close to what I was looking for. In our data model, our 'end' or 'through' date columns are nullable.
Something like the following was needed:
Category.Where(
w => w.ValidFrom <= now &&
(w.ValidThru == null ? DateTime.MaxValue : w.ValidThru) >= now).Select(s => s).Dump();
I found this works when wanting only to compare the data part.
var results = from rate in ct_rates
where rate.StartDate.Date <= date && rate.EndDate.Date >= date
select rate;
something like this?
var result = from context.ct_rate.Where(d => d.start_date >= "2010-10-01 00:00:00" && d.end_date <= "2010-10-01 00:00:00")

Resources