comparing datetime less than or equal to not working - c#

I have a query that gets data from the database. The user can select a date range for getting the data. I want my date range filters to be inclusive, so if a user selects date from and date to, the query should do the following:
all dates >= date FROM
AND
all dates <= date TO
this is working fine for the date FROM, I'm getting everything greater AND equal to the date FROM. But for date TO, its not including the date, just getting everything LESS than the date TO.
here's the code:
query = query
.Where(x => x.TradeDt >= tradeDFrom)
.Where(x => x.TradeDt <= tradeDTo);

TradeDt is likely a datetime, and the time portion isn't midnight, while your tradeDTo's time portion is midnight. Add a day to tradeDTo, and use <
tradeDTo=tradeDTo.AddDays(1);
query = query
.Where(x => x.TradeDt >= tradeDFrom)
.Where(x => x.TradeDt < tradeDTo);

Try to compare only the date part.
query = query
.Where(x => x.TradeDt.Date >= tradeDFrom.Date)
.Where(x => x.TradeDt.Date <= tradeDTo.Date);

Related

Getting Dates from the Month that no records in the table

In my ASP.NET MVC application, I pass the date from the view, and from the controller, I divide it and get the given date Month start date, and end Date.
Then from StartDate to EndDate, I'm checking from the database table to get the data that is available.
Also, I want to get the dates that there are no records in the table that are given date ranges.
Any way of getting those records to list?
This is my code that get the date range and select the data from the database for the start and end dates.
var startDate = new DateTime(attendanceVM.MonthYear.Year, attendanceVM.MonthYear.Month, 1);
var endDate = startDate.AddMonths(1).AddDays(-1);
int EmpId = int.Parse(((System.Security.Claims.ClaimsIdentity) User.Identity).FindFirst("UserId").Value);
IEnumerable < AttendanceResults > resutls = (from l in db.UserLog
where l.Emp_Id == EmpId && l.Login_Date >= startDate && l.Login_Date <= endDate
select new AttendanceResults {
Id = l.Id,
Date = l.Login_Date.ToString(),
Acc_Name = l.PC_User_Name,
PC_Name = l.PC_Name
}).ToList();
You should create a range using Enumerable.Range
var range = Enumerable.Range(0, 1 + endDate.Subtract(startDate).Days).Select(dayOffset => startDate.AddDays(dayOffset));
then you can use the Except function
var notInRange = range.Except(results.Select(r => r.login_Date))
By the way you should review the typo, the formatting and the use of a naming notation

Get records NOT between two date ranges (start and end)

I have a collection list with Lastpayment as a field. Using a date picker i want to filter by Paid and not paid within a given start date and end date.
what i have achieved is getting All paid customers with a start date and end date using
var filterDate = paymentData
.Where(value => value.district != null && (Convert.ToDateTime(value.last_payment_date) >=
Convert.ToDateTime(startdate) && Convert.ToDateTime(value.last_payment_date) < Convert.ToDateTime(enddate))
).ToList();
the above code will filter and give me all customers that have paid within a start and enddate.
However, my challenge is getting the Reverse which is getting all customers that have NOT PAID within a start and enddate.
Can i use a NOT IN expression in LINQ?. How can i adjust my code to give me the reverse ?

How to add hour from another column to date in Linq to SQL

I write that code. I want to use T-SQL DATEADD.
var result = dbContext.MyEntity
.Where(DbFunctions.AddHours(g.Date,g.Hour) >= minDate
&& DbFunctions.AddHours(g.Date,g.Hour) <= maxDate).ToList();
When this gets to SQL, I get the error:
The datepart hour is not supported by date function dateadd for data
type date
I found solution, may be help someone else;
var result = dbContext.MyEntity
.Where(DbFunctions.CreateDateTime(g.Date.Year,g.Date.Month,g.Date.Day,g.Hour,0,0) >= minDate
&& DbFunctions.CreateDateTime(g.Date.Year,g.Date.Month,g.Date.Day,g.Hour,0,0) <= maxDate).ToList();

How works datetime and How to compare two dates in windows form ADO Entities?

I have table "Deposit" in my database sql server with column "DepositDate" and it's type is datetime.
I have rows with todays date and one of them is 2019-02-14 22:26:50.000
And Today's date is the same , I mean 2019-02-14
But when try to get all todays row by following code, it doesn't work. I think it has something with those time that follow the date to do.
But write now I'am intreseted only day but I want to keed datetime type in my database.
So Must even time mutch with date I'am searching? I use Linq and Entities
I do like this , I do not get error but do not find the row. returning empty datagridview
DateTime TodayDate = Convert.ToDateTime(DateTime.Now.ToString());
var deposit = (from u in db.Deposit
where u.DepositDate == TodayDate
select u).ToList();
if (deposit != null)
{
dgvDeposit.DataSource = null;
dgvDeposit.DataSource = deposit;
}
I tried even But I get erroe
date is not supported in LINQ to ADO Entities
DateTime TodayDate = Convert.ToDateTime(DateTime.Now.Date);
where u.DepositDate.Value.Date == TodayDate
I really appreciate your help
You have to filter between a start and end date if you want to make use of whatever indexes you might have on DepositDate. If you do not care about that there are Canonical Date Functions you can use to strip the time from the database value.
DateTime today = DateTime.Today;
DateTime tomorrow = today.AddDays(1);
var deposit = (from u in db.Deposit
where u.DepositDate >= today && u.DepositDate < tomorrow
select u).ToList();
Or as a lambda
var deposits = db.Deposit
.Where(u => u.DepositDate >= today && u.DepositDate < tomorrow)
.ToList();
As a side note and also a matter of opinion it generally recommended to pluralize DbSet<T> properties on your DbContext (like db.Deposits). The same goes for your variable deposit as it will be of type List<Deposit> which is a collection (the 2nd code sample I changed it).

Comparing two dates in LINQ to SQL

I have a database with a table named meeting. Meeting dates are stored in this table using the following format: May 2nd 2011 is (for example) formatted as 5/2/2011.
My requirement is to get the meetings between two dates (say, 4/25/2011 and 5/2/2011) and to write a query comparing a date with these two dates. Does it compare like 4/25/2011 < 4/26/2011? How does the comparison take place?
I am using SQL Server 2008 and LINQ to SQL queries.
Something like this
DateTime start = new DateTime("4/25/2011");
DateTime end = new DateTime("5/2/2011");
var result = db.Meeting.Where(d => d.MeetingDate >= start
&& d.MeetingDate <= end);
Query style:
from m in db.Meetings
where m.Start => start && m.End <= end
select m;
Method style:
db.Meetings.Where(m => m.Start => start && m.End <= end);

Categories