Decode Oracle in Linq to EF - entity-framework

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

Related

runtime Type Error in F# Linq Sql query with group by on multiple columns

I implemented a Linq query in F# that uses this solution to group by multiple columns. It compiles and works half of the time but in the other half of the time the program throws a runtime type miss-match error. Sometimes the AnonymousObject seems to get an int instead of a Nullable<int>, which then causes an error.
let q = query{
for wh in d.Table1 do
where (wh.Date >= vDate)
where (wh.Date <= bDate)
join tae in d.Table2 on
(wh.Table2Key = tae.key)
let key = AnonymousObject<int,int,Nullable<int>>(wh.Table3key,wh.ProjectTableKey,tae.ProjectPhaseKey)
where tae.ProjectPhaseKey.HasValue
groupValBy wh key into g
select {pkey = g.Key.Item2; lphasekey = g.Key.Item3 ; orgk = g.Key.Item1; time = g.Sum (fun x -> x.data) }
}
How can it be, that the types change at runtime? Can anybody give me a hint? Or has an idea how to work around that?

SqlFunctions.DateAdd equivalent in EF 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

Entity Framework - TOP using a dynamic query

I'm having issues implementing the TOP or SKIP functionality when building a new object query.
I can't use eSQL because i need to use an "IN" command - which could get quite complex if I loop over the IN and add them all as "OR" parameters.
Code is below :
Using dbcontext As New DB
Dim r As New ObjectQuery(Of recipient)("recipients", dbcontext)
r.Include("jobs")
r.Include("applications")
r = r.Where(Function(w) searchAppIds.Contains(w.job.application_id))
If Not statuses.Count = 0 Then
r = r.Where(Function(w) statuses.Contains(w.status))
End If
If Not dtFrom.DbSelectedDate Is Nothing Then
r = r.Where(Function(w) w.job.create_time >= dtDocFrom.DbSelectedDate)
End If
If Not dtTo.DbSelectedDate Is Nothing Then
r = r.Where(Function(w) w.job.create_time <= dtDocTo.DbSelectedDate)
End If
'a lot more IF conditions to add in additional predicates
grdResults.DataSource = r
grdResults.DataBind()
If I use any form of .Top or .Skip it throws an error : Query builder methods are not supported for LINQ to Entities queries
Is there any way to specify TOP or Limit using this method? I'd like to avoid a query returning 1000's of records if possible. (it's for a user search screen)
Rather than
r = new ObjectQuery<recipient>("recipients", dbContext)
try
r = dbContext.recipients.
.Skip() and .Take() return IOrderedQueriable<T> while .Where returns IQueriable<T>. Thus put the .Skip() and .Take() last.
Also change grdResults.DataSource = r to grdResults.DataSource = r.ToList() to execute the query now. That'll also allow you to temporarily wrap this line in try/catch, which may expose a better message about why it's erroring.
Mark this one down to confusion. I should have been using the .Take instead of .Top or .Limit or anything.
my final part is the below and it works :
grdResults = r.Take(100)

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

ADO.NET Entity Data Model are not precise enough

I run this code:
var cos = from k in _db.klienci_do_trasy where k.klient_id == 5 select k;
but the query send to database is:
SELECT * FROM `klienci_do_trasy`
LIMIT 0, 30
why is it for, there is no condition on klient_id?
What database are you using? If this is really the case, there could be an error in the provider.