Handling TimeSpan in LINQ where TimeSpan is first referenced in query - c#

I understand that LINQ cannot use TimeSpan, however I want to do a where condition on a DateTime with an added TimeSpan. My Issue is however that the TimeSpan is first referenced within the query. I have tried doing it in memory but that also causes issues.
entiteis is of type:
System.Data.Entity.IDbSet<CharterRequestDTO>
And biddingToCloseIn is defined as:
[NotMapped]
public TimeSpan BiddingToCloseIn
{
get { return TimeSpan.FromTicks(BiddingToCloseInTicks); }
set { BiddingToCloseInTicks = value.Ticks; }
}
Normal:
var charterRequestDtoIds =
(from e in entities
where e.ClientId == clientId
&& e.Status != TrackingState.Void
&& DateTime.Now < e.CreatedAt.AddDays(30).Add(e.BiddingToCloseIn)
select e.Id);
In Memory:
var charterRequestDtoIds =
from e in entities.Where( e => e.ClientId == clientId
&& e.Status != TrackingState.Void
&& DateTime.Now < e.CreatedAt.AddDays(30).Add(e.BiddingToCloseIn) )
select e.Id;
Error for both:
System.NotSupportedException: LINQ to Entities does not recognize the
method 'System.DateTime Add(System.TimeSpan)' method, and this method
cannot be translated into a store expression.

Instead of adding a TimeSpan, add milliseconds (or seconds, minutes, according to the precision you need.)
AddDays(30).AddMilliseconds(e.BiddingToCloseIn.TotalMilliseconds)

Related

Can't compare dates using SQLite with EF6

I'm trying to compare dates using Linq to Entities on a SQLite database. The following code works, but I need to trim off the time portion to get the correct results.
return (from c in Context.Car
join d in Context.Driver on c.CarID equals d.DriverID
join r in Context.Rides on c.CarID equals r.RideID into rideJoin
from rides in rideJoin.DefaultIfEmpty()
where c.IsActive && d.IsActive
group rides by new { c.CarID, d.FullName, d.HireDate, d.FirstPayRiseDate } into grp
select new MyCustomClass
{
CarID = grp.Key.CarID,
Driver = grp.Key.FullName,
NumberOfRides = grp.Count(x => x != null && x.RideDate >= grp.Key.HireDate && x.RideDate <= grp.Key.FirstPayRiseDate)
}).OrderBy(x => x.Driver ).ToList();
I've tried using System.Data.Entity.DBFunctions like so and I get this error:
NumberOfRides = grp.Count(x => x != null && DbFunctions.TruncateTime(x.RideDate) >= grp.Key.HireDate && DbFunctions.TruncateTime(x.RideDate) <= grp.Key.FirstPayRiseDate)
SQL logic error or missing database no such function: TruncateTime
I also get the same error with DBFunctions.DiffDays()
I've also tried casting to Date like so and get this error:
NumberOfRides = grp.Count(x => x != null && x.RideDate.Date >= grp.Key.HireDate && x.RideDate.Date <= grp.Key.FirstPayRiseDate)
'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported
What gives? How am I supposed to do Date functions in Linq to Entities with SQLite??
I need to trim off the time portion to get the correct results
No you don't. If you want to include the rows from startDate through endDate inclusive then just use
... && x.RideDate >= startDate && x.RideDate < endDate.AddDays(1)
(Note that the second comparison is now "strictly less than".)
How are you storing dates on the database ? as unix time integrs ?
in that acse you can amend your connection string to include this following config setting and it will make it easy to read the datetime value via EF.
datetimeformat=UnixEpoch;datetimekind=Utc
So something like :
data source="|DataDirectory|\data.sqlite";datetimeformat=UnixEpoch;datetimekind=Utc
Ref: https://stackoverflow.com/a/24323591/3660930

Short Date in Linq

I want my query to stop displaying time and just the date. This is what I've tried to far:
Query= (from z in ctx.Interactions
where z.ActivityDate <= StartDateTo
&& z.ActivityDate >= EndDateTo
&& z.Indepth == false
select new
{
Date = new DateTime(z.ActivityDate.Year, z.ActivityDate.Month, z.ActivityDate.Day),
Subject = z.Subject
}).ToList();
And
Query= (from z in ctx.Interactions
where z.ActivityDate <= StartDateTo
&& z.ActivityDate >= EndDateTo
&& z.Indepth == false
select new
{
Date = z.ActivityDate.Date,
Subject = z.Subject
}).ToList();
And both didn't work.
LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression. when trying to apply a string method.
You can use anyDate.ToString("ddMMyyyy");//any preferred format.
Not sure if that is what you are looking for!
Your queries return objects with Date & Subject properties.
In the Date property you are passing a DateTime object. In order to display the short date you have a "ToShortDateString()" function on a date.
If you dont want to work with a date and prefer selecting a string, then do the conversion inside the linq query.
Use this if you want to return strings:
var q = (from z in ctx.Interactions
where z.ActivityDate <= StartDateTo && z.ActivityDate >= EndDateTo && z.Indepth == false
select new { Date = z.ActivityDate.Date.ToShortDateString(), Subject = z.Subject }).ToList();
You would need to perform the formatting at the time of the binding. As you don't show the actual binding code, it is hard to specifically address your situation but lets look at what happens in your query:
Query= (from z in ctx.Interactions
where z.ActivityDate <= StartDateTo && z.ActivityDate >= EndDateTo && z.Indepth == false
select new { Date = z.ActivityDate.Date, Subject = z.Subject }).ToList();
Once LINQ handles this query, the resulting Query variable should be of type List<DateTime>. The way you have the query working you would return a list of DateTimes in a format like this:
2014-04-23 00:00:00
2014-03-28 00:00:00
etc...
In order to bind this without the time value, you need to call ToString() on each element (or the desired element) of the list at the time of binding.
Assuming you are using a ListBox or something similar you could write the following:
foreach (var date in myList) //this is the resultant list from the query
{
listBox1.Items.Add(date.ToString("MM/dd/yyyy");
}
If you are literally binding to a DataSource property, you will need to convert your List<DateTime> to a List<string> with the formatted values.
ToShortDateString() may help you.
Query= (from z in ctx.Interactions
where z.ActivityDate <= StartDateTo
&& z.ActivityDate >= EndDateTo
&& z.Indepth == false
select new
{
Date = z.ActivityDate.ToShortDateString(),
Subject = z.Subject
}).ToList();
convert date into string like below
string stringDate=string.empty;
stringDate=Convert.ToDateTime("2014-04-23 00:00:00").ToShortDateString();
it will give output like
2014-04-23

Entity Framework DateTime Functions

I'm trying to filter by date based on two datetimes. MinTime and MaxTime are both nullable DateTimes in SQL, I want to filter based on these, but I'm getting an error:
This function can only be invoked from LINQ to Entities
public static IEnumerable<ExclusionRule> GetExclusionRules(AppointmentTypes? apptType, string resourceName, TimeSpan? minTime, TimeSpan? maxTime, DayOfWeek? day) {
using (var db = new BusinessObjectContainer()) {
db.ExclusionRules.MergeOption = MergeOption.NoTracking;
var items = db.ExclusionRules;
int intDay = day.HasValue ? (int) day.Value : -1,
intApptType = apptType.HasValue ? (int)apptType.Value : -1;
var filteredApptType = apptType.HasValue ? items.Where(i => !i.t_AppointmentType.HasValue|| i.t_AppointmentType == intApptType) : items;
var filteredResource = string.IsNullOrWhiteSpace(resourceName) ? filteredApptType : filteredApptType.Where(i => !string.IsNullOrEmpty(i.ResourceName) && resourceName.ToLower().Equals(i.ResourceName.ToLower()));
IEnumerable<ExclusionRule> filteredMinDate;
if (minTime.HasValue) {
filteredMinDate = filteredResource.Where(i => (!i.MinTime.HasValue) || (EntityFunctions.CreateTime(i.MinTime.Value.Hour, i.MinTime.Value.Minute, 0) >= minTime.Value));
} else {
filteredMinDate = filteredResource;
}
IEnumerable<ExclusionRule> filteredMaxDate;
if (maxTime.HasValue) {
// this throws the exception
filteredMaxDate = filteredMinDate.Where(i => (!i.MaxTime.HasValue) || EntityFunctions.CreateTime(i.MaxTime.Value.Hour, i.MaxTime.Value.Minute, 0) <= maxTime.Value);
} else {
filteredMaxDate = filteredMinDate;
}
var filteredWeekDay= day.HasValue ? filteredMaxDate.Where(i => !i.t_DayOfWeek.HasValue|| i.t_DayOfWeek == intDay) : filteredMaxDate;
return filteredWeekDay.ToList();
You are using EntityFunctions* in a linq statement on an IEnumerable<ExclusionRule>. But you can only use it on an IQueryable that has an Entity Framework query provider. So you should start with IQueryable<ExclusionRule> (from EF) or just create DateTimes in the regular.Net way.
*DbFunctions as of Entity Framework 6.
MinTime.Value.Hour etc. might have to be executed in a Query and it seems to me that you are excecuting them in .Net memory. That's why you get the error.
LINQ to entities:
http://msdn.microsoft.com/en-us/library/bb386964.aspx
This function can only be invoked from LINQ to Entities
EntityFunctions is applicable is LINQ to Entities queries only.
The EntityFunctions class contains methods that expose canonical functions to use in LINQ to Entities queries. http://msdn.microsoft.com/en-us/library/dd456873.aspx
I got around this by calling .ToList() before the DateTime filtering. This pulls everything into .NET so the querying can be done there.
As there are a relatively small number of items in my database this wasn't a problem.

LINQ to Entities with AddMonth method

This is my code:
return Newsletterctx.Subscribers.Count(o =>
o.Validated == false &&
o.ValidationEmailSent == true &&
o.SubscriptionDateTime.AddMonths(1) < DateTime.Now);
I get this error:
LINQ to Entities does not recognize
the method 'System.DateTime
AddMonths(Int32)' method, and this
method cannot be translated into a
store expression.
You can use SqlFunctions classvar;
today = DateTime.Now; return Newsletterctx.Subscribers.Count(o =>
o.Validated == false &&
o.ValidationEmailSent == true &&
SqlFunctions.DateAdd("month",1,o.SubscriptionDateTime) <today);
Perhaps you can shift the date to test against instead:
DateTime testDate = DateTime.Now.AddMonths(-1);
return Newsletterctx.Subscribers.Count
(o => o.Validated == false
&& o.ValidationEmailSent == true
&& o.SubscriptionDateTime < testDate);
You have to use the Datetime outside the request because you are in the LINQ TO ENTITIES that don't use System.Datetime Library.
If you wish to use a fix date the you can define it outside the request as
DateTime compareDate = DateTime.Now.AddMonths(x);
Now, EntityFramework, Version=6 above, you can jus use System.Data.Entity.DbFunctions
return Newsletterctx.Subscribers.Count(o =>
o.Validated == false &&
o.ValidationEmailSent == true &&
DbFunctions.AddMonths(o.SubscriptionDateTime,1) < DateTime.Now);
However, in this case, use the temp variable testDate that answered by Fredrik Mörk uses less resources.

Custom function in Entity Framework query sometimes translates properly, sometimes doesn't

I have this function:
public static IQueryable<Article> WhereArticleIsLive(this IQueryable<Article> q)
{
return q.Where(x =>
x != null
&& DateTime.UtcNow >= x.PublishTime
&& x.IsPublished
&& !x.IsDeleted);
}
And it works just fine in this query:
from a in Articles.WhereArticleIsLive()
where a.Id == 5
select new { a.Title }
But it doesn't work in this only slightly more complex query:
from s in Series
from a in Articles.WhereArticleIsLive()
where s.Id == a.SeriesId
select new { s, a }
I get this error message:
NotSupportedException: LINQ to Entities does not recognize the method 'System.Linq.IQueryable1[TheFraser.Data.Articles.Article] WhereArticleIsLive(System.Linq.IQueryable1[TheFraser.Data.Articles.Article])' method, and this method cannot be translated into a store expression.
Any idea why? Is there another way to consolidate query parameters like this?
Thanks in advance.
EDIT: corrections by Craig.
I'm leaving this here, because I think it's a valuable tool: Use linqkit! But not for solving this question though :-)
Instead of returning IQueryable, use Expression to factor out predicates. E.g. you could define the following static method on Article:
public static Expression<Func<Article,bool>> IsLive()
{
return x =>
x != null
&& DateTime.UtcNow >= x.PublishTime
&& x.IsPublished
&& !x.IsDeleted
}
Then, ensure to store a reference to this expression when building your query, something along the lines of (not tested):
var isLive = Article.IsLive();
from s in Series
from a in Articles.Where(isLive)
where s.Id == a.SeriesId
select new { s, a }

Categories