SqlFunctions.DateAdd equivalent in EF Core - entity-framework-core

I am converting a project to .Net Core and am looking for an equivalent parse operation for a string to SQL in EF core to be used as a predicate (DateAdd).
items.Where(x => SqlFunctions.DateAdd("day", 0, x.ToDateString) <= toDate)
No luck in EF.Functions. Is there an equivalent?
Cheers
KH

Use .NET code instead.
items.Where(x => x.ToDateString.AddDays(0) <= toDate)
EDITED for checking 200,000 records performance

You can change your equation and use
EF.Functions.DateDiffDay(x.ToDateString - toDate) <= 0

Related

Decode Oracle in Linq to EF

I need to convert the code bellow to Linq to EF.
How can I do it?
FROM
ACECPGER_OEE.META_OEE M
,ACECPGER_OEE.PR_EQUIPAMENTO P
WHERE
DECODE(M.COD_EQPMT_PRODC, 'FP1', 'FPA', M.COD_EQPMT_PRODC) = P.COD_EQPMT_PRODC

How to use binary flags in Core Data?

I have an int32 attribute in a Core Data database.
I use this int as an enum bit field.
Is it possible to create a NSPredicate to query items based on the binary
value of this int ? Something like #"bitFieldAttribute & 0x0001"?
I'm also wondering if this is possible with a binary typed attribute ?
NSPredicate can handle it, but I'm not sure if CoreData will accept it as a valid predicate for execution on a data store. It might have trouble converting the bitwise operator into a SQL query (if you're using a SQLite backing store). You'll just have to try it.
The syntax, however, is just what you'd expect:
NSPredicate * p = [NSPredicate predicateWithFormat:#"(3 & 1) > 0"];
NSLog(#"%#", p);
NSLog(#"%d", [p evaluateWithObject:nil]);
Logs:
3 & 1 > 0
1
As for doing this on a binary-typed attribute (ie, one defined as data, right?) This probably won't work. Bitwise operators only really make sense when operating on integers (insofar as I understand them), so executing it on an NSData wouldn't make much sense. Convert it to a number first, and then it might work.
edit
It would appear that SQLite supports this syntax, since bitwise operators have been around since 2001, which means that Core Data will probably accept it as well.
rockfakie is so nearly right but
NSPredicate *someTypePredicate = [NSPredicate predicateWithFormat:#"(typeValue & %i) == %i", valueOneOrThree,valueOneOrThree];
Is what I needed.
Here is one example / application of this technique.
Say you have a NSManagedObject that has an integer attribute with the keypath "typeValue".
Somewhere in your code define a bitwise enumeration:
typedef enum SomeType {
SomeTypeValueOne = 0x1,
SomeTypeValueTwo = 0x2,
SomeTypeValueThree = 0x4
} SomeType;
Now to query for managed objects that are of type say One or Three but not Two, do the following:
SomeType valueOneOrThree = SomeTypeValueOne | SomeTypeValueThree;
NSPredicate *someTypePredicate = [NSPredicate predicateWithFormat:#"(typeValue & %i) == typeValue", valueOneOrThree];
// construct NSFetchRequest as normal with predicate...
I hardly doubt it.
But you may use an enum for the values stored in the attribute, and use a direct comparison instead of a bit masking.

Add a Date in Linq to Entities

With Linq to Entities, I am trying to query a Log table to find rows near a matching row. I am having trouble with adding a date inside the query. This is what I have so far.
from
l in objectSet.Logs
let
match = objectSet.Logs.Where(whatever).FirstOrDefault()
where
l.Timestamp > (match.Timestamp - twoHours)
&& l.Timestamp < (match.Timestamp + twoHours)
select
l
Leaving out the "whatever" condition that finds the row I'm interested in, "twoHours" has variably been a time span, a .AddHours() function and so forth. I haven't found the right way that EF can generate SQL that adds the value from a field (match.Timestamp) to a constant.
The obvious solution is to do the "match" query first and then use the literal value in a second query, but I have simplified the code example here to the main problem (adding dates in the query) and in actual fact my query is more complex and this would not be ideal.
Cheers
You can generate an AddHours using the EntityFunctions class.
from
l in objectSet.Logs
let
match = objectSet.Logs.Where(whatever).FirstOrDefault()
where
(l.Timestamp > EntityFunctions.AddHours(match.Timestamp, -1 * twoHours))
&& // ...
select
l
However, don't expect this WHERE to be optimized with an index unless you have an expression index on the column.
EntityFunctions is deprecated in favor of DbFunctions
public int GetNumUsersByDay(DateTime Date)
{
using (var context = db)
{
var DateDay = new DateTime(Date.Year, Date.Month, Date.Day);
var DateDayTomorrow = DateDay.AddDays(1);
return context.Users.Where(m => DbFunctions.AddHours(m.DateCreated,-5) >= DateDay && m.DateCreated < DateDayTomorrow).Count();
}
}
As it was described in this article - http://www.devart.com/blogs/dotconnect/?p=2982#first, use parameters (declare variable) instead of DateTime using in your queries.

NHibernate Delete with date arithmatic using HQL

I've looked around and can't find too much. But is it possible to do something like this using HQL in nHibernate:
Session.CreateQuery(#"DELETE FROM MyObject mo
WHERE (mo.AlteredDate + mo.ExpiryDetails.ExpiryTimestamp) < :pNow")
.SetDateTime("pNow", DateTime.Now);
So basically I want to delete all MyObjects from the database where the last time the object was altered (mo.AlteredDate - a DateTime) plus an amount of time such as 2 days and 5 hours (ExpiryDetails.ExpiryTimestamp) is less than now.
Or is it best to retrieve the objects and do the caculation in code using the .NET framework?
Super late to answer, but I did something like this & it works:
IQuery query = Session.CreateQuery("select x from OBJECT x where x.DateTimeForCompare > :dateTimeForCompare2");
query.SetDateTime("dateTimeForCompare2", DateTime.Today);
IList<OBJECT> xx = query.List<OBJECT>();

Is there a function similar to Math.Max for Entity Framework?

I have an entity framework query as follows;
From T In Db.MyTable
Where (T.Col1 - T.Col2) + T.Col3 - T.Col4 > 0 _
Select T
I now need to make sure that the bracketed part '(T.Col1 - T.Col2)' does not go below zero.
In .Net, I'd code it as follows (but obviously EF does not like Math.Max).
From T In Db.MyTable
Where Math.Max(T.Col1 - T.Col2,0) + T.Col3 - T.Col4 > 0 _
Select T
Is there an easy way to do this? I am using EF 2.0 (not the latest, just released version).
Thanks in advance
Max isn't supported, but Abs is; will that do? Otherwise you'll have to use a ternary expression. In C#, I'd do:
from t in Db.MyTable
let m = t.Col1 >= t.Col2 ? t.Col1 - t.Col2 : 0
where m + t.Col3 - t.Col4 > 0
However, this will be inefficient at the DB level unless you have an expression index. So I'd suggest a computed column instead.