I have a DateTime column in a SQL Server database. The data stored as follows:
2020-10-04 23:45:00.527
I tried to compare the date as follows with current date (Date in database should be less than current date)
DateTime today = DateTime.Now.Date;
var result = (from c in TableName
where (c.Email == email) &&
c.Password == password && c.Status == 1 &&
c.ValidTill.Date <= today
select c).ToList();
But unfortunately I get this exception
'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported
Even tried the below method, that doesn't seem to work:
DbFunctions.TruncateTime(c.ValidTill)
My comparison would be like this -
2020-10-04 23:45:00 (Datetime in database) <= 2020-10-10 23:00:00 (current date & time)
You don't need .Date
c.ValidTill <= today
or comparing only with date
EntityFunctions.TruncateTime(x.ValidTill) <= today.Date
needs to do the work.
var today = DateTime.Now;
var result = (from c in TableName
where (c.Email == email) &&
c.Password == password && c.Status == 1 &&
c.ValidTill <= today
select c).ToList();
hope it will work.
I am trying to execute the following code and am receiving an error
public List<Log> GetLoggingData(DateTime LogDate, string title)
{
var context = new LoggingEntities();
var query = from t in context.Logs
where t.Title == title
&& t.Timestamp == LogDate
select t;
return query.ToList();
}
The error I'm receiving is "The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported." I have tried various attempts of casting everythign to a string, only comparing the date part, but can't seem to get the right combinaation. Any help is greatly appreciated.
If you are using EF 6.0+, you can use DbFunctions.TruncateTime(DateTime?) :
var query =
from t in context.Logs
where t.Title == title
&& DbFunctions.TruncateTime(t.Timestamp) == LogDate.Date
select t;
Note: For earlier version of EF where DbFunctions isn't available, EntityFunctions.TruncateTime(DateTime?) can be used instead.
Not the greatest solution, but it works. For a variety of reasons, I have to use .net 3.5 at this point and modifying the database would be difficult. Anyways, here is a solution that works:
var query = from t in context.Logs
where t.Title == title
&& t.Timestamp.Day == LogDate.Day
&& t.Timestamp.Month == LogDate.Month
&& t.Timestamp.Year == LogDate.Year
select t;
Not the most elegant solution, but it is effective.
EntityFunctions.TruncateTime(t.Timestamp) is obsolete from EF6.
Use below
DbFunctions.TruncateTime(t.Timestamp)
Always use EntityFunctions.TruncateTime() for both x.DateTimeStart and LogDate.
such as :
var query = from t in context.Logs
where t.Title == title
&& EntityFunctions.TruncateTime(t.Timestamp) == EntityFunctions.TruncateTime(LogDate)
select t;
Correct me if I'm wrong, but in mikemurf22's example, it would need to check each part of the date component, and potentially a lot more server processing?
Anyway, I stumbled across this problem, and this is my solution.
Assuming that you're going to be passing in the date component only, you can find the last minute of the day that you pass in, and use the where clause to define the range.
public List<Log> GetLoggingData(DateTime LogDate, string title)
{
DateTime enddate = new DateTime(LogDate.Year, LogDate.Month, LogDate.Day, 23, 59, 59)
var query = from t in context.Logs
where t.Timestamp >= date
where t.Timestamp <= enddate
select t;
return query.ToList();
}
Convert LongDate to .ToShortDateStringand then you can use it this way:
EntityFunctions.TruncateTime(t.Timestamp) == LogDate
like mike did
Try this:
var calDate = DateTime.Now.Date.AddDays(-90);
var result = return (from r in xyz where DbFunctions.TruncateTime(r.savedDate) >= DbFunctions.TruncateTime(calDate)
You can use this hack:
DateTime startDate = LogDate.Date;
DateTime endDate = LogDate.Date.AddDays(1);
var query = from t in context.Logs
where t.Title == title
&& t.Timestamp >= startDate
&& t.Timestamp < endDate
select t;
I've a IQueryable result queried from the db using LINQ, now I had to filter all the rows based on a field date_sent. No. of days should be calculated from date_sent to current date. this total no. of days that falls undet 0 to 30 days range should be retrieved. How do I do it. I have the below code but it's not working. no errors but does not filter properly.
query = query.Where(x => x.DATE_SENT != null);
query = query.Where(x => (int)(EntityFunctions.DiffDays(currentDate, (DateTime)x.DATE_SENT)) >= 0 &&
(int)(EntityFunctions.DiffDays(currentDate, (DateTime)x.DATE_SENT)) <= 30);
Any guidance on how it can be handled will be great.
You can get it using this query.
var dtDiff = DateTime.Now.AddDays(-30);
query = query.Where(z=> z.DATE_SENT >= dtDiff);
31-60 days
var dtDiff31 =DateTime.Now.AddDays(-31);
var dtDiff60 =DateTime.Now.AddDays(-60);
query = query.Where(z=> z.DATE_SENT >= dtDiff60 && z.DATE_SENT <= dtDiff31 );
It seems you just want to filter by a range of dates:
DateTime filterDate = currentDate.AddDays(-30);
query = query.Where(x => x.DATE_SENT >= filterDate);
In case your currentDate can vary you add the second condition:
query = query.Where(x => x.DATE_SENT >= filterDate && x.DATE_SENT < currentDate);
I have a problem; actually I am having workingHours in my database table in the format HH.mm and I want to sum all the working hours using LINQ can any 1 please tell how to do this.
In totalWeekHours I have all the working hours and I have replace HH.mm format in HH:mm but I don't know how to parse it in timespan and then Sum() using Linq.
please help.
var totalWeekHours = (from twh in db.MytimeMaster
where ((twh.date >= lstsun && twh.date <= tilldate)
&& (twh.agentID == agentid))
select twh.totalworkinghours).ToList();
if (totalWeekHours.Count > 0)
{
List cnvrtToTimespanlist = new List();
foreach(var list in totalWeekHours)
{
cnvrtToTimespanlist.Add(list.ToString().Replace('.', ':'));
}
}
You can use Aggregate method.
var sum = (from twh in db.MytimeMaster
where ((twh.date >= lstsun && twh.date <= tilldate) && (twh.agentID == agentid))
select twh.totalworkinghours).Aggregate(TimeSpan.FromMinutes(0), (total, next) => total + next);
p.s. assume used TimeSpan for time intervals.
I'm using linq-to-sql for a query like this:
public static List<MyModel> GetData(int TheUserID, DateTime TheDate)
using (MyDataContext TheDC = new MyDataContext())
{
var TheOutput = from a in TheDC.MyTable
where a.UserID == TheUserID
(where a.Date1.Month == TheDate.Month && where a.Date1.Year == TheDate.Year)
OR
(where a.Date2.Month == TheDate.Month && where a.Date2.Year == TheDate.Year)
group a by a.Date1.Date AND by a.Date2.Date into daygroups
select new MyModel{...};
How do I write this to make the OR and the AND statement work? I've tried putting a || and a && in place but it doesn't work and I'm stuck on this query.
Basically, it should return a list of days within a month and in the MyModel, I do counts. For instance, in a column I count the number of appointments set on a given day and in another column I count the number of appointments attended on the same day. Date1 refers to the date the appointments are set and Date2 refers to the dates the appointments are attended. So for example, on March 3rd 2011, I've set 4 appointments (Date1 is 3/11/2011 for these) and they're set for various dates in the future (Date2). During the same date (March 3rd is Date2 this time), I've also attended several other appointments that were set on other dates in the past.
Thanks for any suggestions.
using (MyDataContext TheDC = new MylDataContext())
{
var TheOutput = from a in TheDC.MyTable
where a.UserID == TheUserID &&
(a.Date1.Month == TheDate.Month && a.Date1.Year == TheDate.Year)
||
( a.Date2.Month == TheDate.Month && a.Date2.Year == TheDate.Year)
group a by a.Date1.Date AND by a.Date2.Date into daygroups
select new MyModel{...};
Try removing the 4 extra "where" and change the OR to ||.