Expressing IN sql statement in Linq with computed string in where clause - sql

I'm trying to build a linq query for the following sql query :
SELECT *
FROM [QryFiles2]
WHERE (left(Demande,6) IN (select NoBVR from tblRequest))
Knowing that QryFiles2 is an entity and tblRequest too I've come to something like that :
from f in db.QryFiles2
where f.Demande.Substring(0,6) /* in (select NoBVR from tblRequest) */
select f
The thing is I don't know how I could express the in sql statement using linq. Do you know how I could do that without using raw sql queries ?
Nb : I've tried to use the .contains method but I couldn't work it out with the computed substring.

Finally I've done this using a join statement like that :
from f in db.QryFiles2
join r in db.tblRequest on f.Demande.Substring(0,6) equals r.NoBVR
Super clean super fast no need for subqueries should have thought about that before :-)

Related

How to convert Sql inner query to Linq for sum of column

How to Convert this sql query to Linq.
select sum(OutstandingAmt)from IvfReceiptDetails where IvfReceiptId IN(select IvfReceiptId from IvfReceipts where PatientId = 'SI-49650')
I think it is easier to translate SQL using query comprehension syntax instead of lambda syntax.
General rules:
Translate inner queries into separate query variables
Translate SQL phrases in LINQ phrase order
Use table aliases as range variables, or if none, create range variables from table names
Translate IN to Contains
Translate SQL functions such as DISTINCT or SUM into function calls on the entire query.
Here is the code:
var IvfReceiptIds = from IvfReceipt in IvfReceipts
where IvfReceipt.PatientId = "SI-49650"
select IvfReceipt.IvfReceiptId;
var OutstandingAmtSum = (from IvfReceiptDetail in IvfReceiptDetails
where IvfReciptIds.Contains(IvfReceiptDetail.IvfReceiptId)
select IvfReceiptDetail.OutstandingAmt).Sum();
Try this, First get all IvfReceiptId in array based on your inner query used in where condition then check contains. Change name of your _context if it's different.
var arrIvfReceiptId = _context.IvfReceiptDetails.Where(p=>p.PatientId == "SI-49650").ToArray();
var sum = (from ird in _context.IvfReceiptDetails.Where(p=> arrIvfReceiptId.Contains(p.IvfReceiptId))
select OutstandingAmt).Sum();

How to convert these SQL script code to CE function in SAP HANA

I want to transfer his SQL script code to CE functions:
select
A.MANDT,
A.ORGEH,
A.ORGLV,
A.S_PLANS,
A.S_PERNR,
b.ENAME as ENAME,
(case
when a.ORGLV = 'ROOT' then 'ROOT - ALL Chief'
when a.S_PERNR is null then ''
when b.ENAME is null then a.S_PERNR
else a.S_PERNR||' - '||b.ENAME end) as CA_LV_CHIEF
from : "Table1" as a left outer join "Table2" as b
on b.PERNR = a.S_PERNR
AND A."MANDT" = B."MANDT"
group by A.MANDT, A.ORGEH, A.S_PERNR, A.S_PLANS, A.ORGLV, b.ENAME
My question is about the calculated attribute CA_LV_CHIEF. You can see there are multiple when...then statements in the original SQL. I want to use CE_CALC() function to convert it to CE function, but I don't know how to nest CASE statement in it.
If possible, how can I do it; or if not possible, is there any other way to convert it to a CE function?
Thanks
The correct answer to this question is: don't do it!
Using CE functions has been clearly disencouraged by SAP.
With your SQL statement you have an easy to understand, straight forward to maintain and good optimizable statement.
Don't make it worse in every of those aspects by rewriting it as a CE function call.

Convert mysql query to HQL

select distinct gv.geoname,gv.GeoHierLevelCode,
SUBSTRING_INDEX(SUBSTRING_INDEX(gd.geohierpath,'/',4),'/',-1)
from HavellsUserSalesForceMapping hs,geohiervalue gv,geohierpathdetails gd
where hs.userCode = '00000001'
and hs.cmpCode = gv.cmpCode
and gv.geoCode IN (hs.geoCode)
and gd.geohierpath LIKE CONCAT('%/',gv.geoCode,'/%') ;
SUBSTRING_INDEX is getting issue for me.Tell me the actual way to convert to hql
Use #Formula annotation and define a dummy field for your expression. Then the field could be used in HQL query.

chain string expression linq

In traditional sql we can chain expression according to if statements.
for example lets say I have variable called "firstName" and I want to get from database all users according to the value in this variable(if empty get all users)
so I will chain the sql string like that
string sql="";
if(firstname!="")
sql=String.format(" And firstname='{0}',firstName)
.ExecuteReader(System.Data.CommandType.Text,"select * from users where 1=1" + sql)
Is there a way to copy this Technique to linq expression?
something like
from U in user
where 1=1 & sql
select U
Change to method syntax instead of query syntax, and chaining is easy.
var query = user.Select(u => u);
if(firstname!="")
query = query.Where(u => u.firstname = firstname);
queries in query syntax are converted at compile-time, so there's not a mechanism to "inject" sql at run time using query syntax.

Linq to sql constantly returns null

I have the following SQL query:
SELECT DISTINCT
Participant.BackgroundTrainingID,
Location.TrainingSite
FROM Registration, ProgramLocation, Participant, Program, Location
WHERE ProgramLocation.LocationID = Location.LocationID
AND ProgramLocation.ProgramID=Registration.ProgramID
AND Registration.ParticipantID=Participant.ParticipantId
I wrote the following LINQ to SQL to match the query above:
var trainingsiteinfo = (from c in db.ProgramLocations
from n in db.Registrations
from l in db.Participants
from h in db.Locations
where c.LocationID == h.LocationID
&& c.ProgramID == n.ProgramID
&& n.ParticipantID == l.ParticipantId
select new {
h.TrainingSite,
l.BackgroundTrainingID }).Distinct();
The SQL query works fine but LINQ constantly returns null.
I use Linqer http://www.sqltolinq.com/ when i get out of ideas :-) Aswell it speeds up conversion jobs.
Make sure your db context isn't going out of scope before you have bound your results. You can test this by adding .ToList() after your .Distinct() to force eager loading of the results.
Your linq code has an unclosed } bracket in the select statement