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);
Related
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();
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).
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);
I'm using entity framework to work with a MySql database. I have a DateTime column that I want to query and return all rows where the date portion of the DateTime column's values are within the range of the current day.
I'm assuming the DateTime property is not normalized to day, so that it may have any value throughout the day.
The most straightforward way staying in Entity Framework land is probably something like:
DateTime today = DateTime.Today; // earliest time today
DateTime tomorrow = DateTime.Today.AddDays(1); // earliest time tomorrow
var q = db.Objects
.Where(x => x.Time >= today)
.Where(x => x.Time < tomorrow);
This is a general technique that can find any range of times (not just specific days).
If the table has or will have many rows, you'll want to check that it is indexed to prevent having to scan the entire table.
Using LINQ to Entities:
private readonly Entities _db = new Entities();
var entities = (from t in _db.table
where t.dateColumn.Date.Equals(DateTime.Today)
select t);
Like this?
SELECT ??? FROM YourTable WHERE DATE(dateTime) = DATE(NOW());
I think he means where the date part of the datetime field is today's date, correct?
In that case something like this would work:
SELECT CAST(yourdatecol AS DATE) FROM thetable WHERE CAST(yourdatecol AS date) = CURDATE();
I have the following query that returns the login count per day from a given date.
var sot = from uts in DataContext.UserTrackingStatistics
let startDate = new DateTime(2009, 10, 01)
where uts.LastLogin >= startDate
group uts by uts.LastLogin.Date into myGroup
orderby myGroup.Key.Date
select new { Count = myGroup.Count() , myGroup.Key.Date};
I would like this to say the count was 0 for a given day rather than not return anything. How could I do that within this query?
You can't do it just with LINQ-to-SQL, as you'd have to use a union on your query with data that doesn't actually exist, which LINQ-to-SQL can't do.
To do this, you'll need to fill in the gaps client-side. I'm not in front of VS at the moment, but a general approach would be this:
Define your date range (since you mention no end date in your code and we're talking about login date, I'm assuming that the end date would be the current date.
Use Enumerable.Range to create a list of numbers ranging from 0 to the number of days within your date range, then use Select to transform that list into a list of dates. Select your results using an anonymous type and use the same properties as your L2S statement; this way, the compiler will reuse the same type
Combine your lists together using an outer join (not the most obvious syntax in LINQ, unfortunately) on the Date property
Order your results by date
This will now show 0 for the gaps.
I'll try to post a code sample below, but note that I can't compile where I am, so it may require tweaking.
var allDates = Enumerable.Range(0, (DateTime.Today - startDate).TotalDays)
.Select(i => new { Count = 0, Date = startDate.AddDays(i) });
var fullResults = from d in allDates
join r in results on d.Date == r.Date
from oj in r.DefaultIfEmpty()
select new { Count = oj == null ? 0 : oj.Count, Date = d.Date };