Linq simple query improvement - c#

I'm a beginner in Linq queries and I'm wondering if my query can be improved one way ore another:
long vehid = json.VehicleId.Value;
DateTime date = DateTime.Parse(json.date.Value);
var Alerts = (from t2 in entities.Alerts.AsNoTracking()
where
t2.GeneratedTimeLocal.Year == date.Year
&& t2.GeneratedTimeLocal.Month == date.Month
&& t2.GeneratedTimeLocal.Day == date.Day
&& (t2.AlertType == 2 || t2.AlertType == 3)
&& t2.vId == vid
select new
{
GeneratedTimeLocal = t2.GeneratedTimeLocal,
OptionalText = t2.OptionalText
});
return Alerts;
The problem is that the Alerts datatable has a huge amount of data in it that increases day by day and right now it's kind of slow.
The GeneratedTimeLocal field from Alerts datatable is type datetimeoffset(7).
Is there a way to improve this query?

Define a date range to improve the query. Then check the query execution plan and based on that decide if you need a new index or change existing indexes.
long vehid = json.VehicleId.Value;
DateTime dateFrom = DateTime.Parse(json.date.Value).Date; // date with no time
DateTime dateTo = dateFrom.AddDays(1); // add one day to create the date range
var Alerts = (from t2 in entities.Alerts.AsNoTracking()
where
t2.GeneratedTimeLocal >= dateFrom
&& t2.GeneratedTimeLocal <= dateTo
&& (t2.AlertType == 2 || t2.AlertType == 3)
&& t2.vId == vid
select new
{
GeneratedTimeLocal = t2.GeneratedTimeLocal,
OptionalText = t2.OptionalText
});
return Alerts;
On the other hand, remember that this query won't be executed until you do a ToList(), for example.
Try this index:
CREATE INDEX IX_Alert_GeneratedTimeLocal_vId_AlertType_with_include ON Alert(GeneratedTimeLocal, vId, AlertType) INCLUDE(OptionalText)
I'm assuming you're using SQL Server. You could also try a filtered index if the table is huge. Check out this link: https://learn.microsoft.com/en-us/sql/relational-databases/indexes/create-filtered-indexes

Related

Comparison of Date From SQL Server and C# DateTime

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.

How to remove Time part of Date in MySQL with Entity Framework [duplicate]

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;

Retrieve rows between the date range based on total days

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

Sum time intervals using linq

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.

linq where statement with OR

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

Categories

Resources