Sql to Linq. how can i translate? Is there a translation program? - sql

SELECT Musteriler.MusteriAdiSoyadi,Sum(Odemeler.OdemeTutari) AS ToplamOdeme FROM Odemeler
Right JOIN Musteriler ON Odemeler.OdeyenId = Musteriler.Id
GROUP BY Musteriler.MusteriAdiSoyadi,Musteriler.KayitTarihi ORDER BY Musteriler.MusteriAdiSoyadi;
How do I convert the running sql query to linq

Related

How to convert and optimize this SQL query in SqlKata

I have this SQL query and I need to convert it SqlKata
SELECT VVIAGGIO AS VIAGGIO, ISNULL(AEAN,'') AS EAN
FROM
SALDI V INNER JOIN ARTICOLI A ON VARTI=AARTI
INNER JOIN ORDI OT ON VSTAB=OTSTAB AND VMAGA=OTMAGA AND VAGG=OTRAGG
WHERE VORDI ='21'
AND VHOST ='68'
ORDER BY VPROG, AARTI
I don't know how to structure it because here is ISNULL(), INNER JOIN..
I already checked SqlKata select instruction.
Any suggestions on how to optimize and convert this query in SqlKata?
here is what you need:
var query = new Query("SALDI as V")
.Join("ARTICOLI as A","A.ARTI","V.ARTI")
.Join("ORDI as OT",j => j.On("OT.STAB","V.STAB")
.On("V.MAGA","OT.MAGA")
.On("V.AGG","OT.RAGG")
)
.Where("V.ORDI","21")
.Where("V.HOST","68")
.Select("V.VIAGGIO")
.SelectRaw("ISNULL(V.AEAN,'') as EAN")
.OrderBy("V.PROG", "A.ARTI")

SQL query to LINQ how to write query in linq

select *
from impact
where (usedin&1)=1
and impactid not in
(select impactid
from responsetime
where scontractid= scontractid )
I am learning LINQ.
I want to write this query in LINQ.
I'm told that a SQL query like this will be faster.
select *
from impact i
left outer join responsetime r on i.impactid = r.impactid
where (usedin&1)=1
and scontractid= scontractid
and r.impact is null
Converting that to linq, we get:
from i in impact
from r in responsetime.Where(rr=>i.impactid = rr.impactid).DefaultIfEmpty()
where (i.usedin&1)=1
and i.scontractid= scontractid
and r.impact is null
select i

Using LinQ with group by more than one column

I'm Newbi in LinQ, I have problem with group by in linQ.
I wan to query like this:
select
MAX(TCheckpointGrouping.Id) AS CheckpointGroupingId,
MAX(TCheckpointGrouping.MCheckpointId) AS CheckpointId,
MAX(MCheckpoint.Name) AS CheckpointName,
MAX(CAST(MCheckpoint.IsMajor AS VARCHAR)) AS IsMajor,
MAX(TCheckpointGrouping.MIndicatorId) AS IndicatorId,
MAX(MIndicator.Name) AS IndicatorName,
MAX(MCriteria.Id) AS CriteriaId,
MAX(MCriteria.Name) AS CriteriaName,
MAX(MPrinciple.Id) AS PrincipleId,
MAX(MPrinciple.Name) AS PrincipleName,
MAX(TCheckpointGrouping.RelationToCheckPoint) AS RelationToCheckPoint
from TCheckpointGrouping
inner join MCheckpoint on MCheckpoint.Id = TCheckpointGrouping.MCheckpointId
inner join MIndicator on MIndicator.Id = TCheckpointGrouping.MIndicatorId
inner join MCriteria on MCriteria.Id = MIndicator.MCriteriaId
inner join MPrinciple on MPrinciple.Id = MCriteria.MPrincipleId
group by
TCheckpointGrouping.MCheckpointId,
TCheckpointGrouping.MIndicatorId
How can i convert query above into LinQ (VB.NET)
thanks
bestRegards
I'm tempted to convert this SQL query to LINQ for you, but I think that would be a waste of opportunity for you to learn yourself.
There's a great page from Microsoft with lot of VB.NET Linq situations: 101 Linq Samples.
You can even find an example of a Group By using Multiple Columns.
Good learning. :)
I am not sure about this, but you can try it. In select part i have not included all the columns.
var result= from TChkgp in TCheckpointGrouping
join MCpoint in MCheckpoint on TChkgp.Id equals MCpoint.Id
join MIndtor in MIndicator on TChkgp.MIndicatorId equals MIndtor.Id
join MCrteia in MCriteria on MIndtor.Id equals MIndtor.MCriteriaId
join MPrncple in MPrinciple on MCrteia.MPrincipleId equals MPrncple.Id
group TChkgp by new (TChkgp.MCheckpointId,TChkgp.MIndicatorId} into g
select new {
CheckpointGroupingId =TChkgp.Id.Max(),
CheckpointId =TChkgp.MCheckpointId.Max,
....
....
};
you can see one simple example on following link
Group and sum in linq

How can I eliminate the subquery from this sql query?

I have a query which should work, but it seems I am a victim of a poor database technology. I need to run the query below on a Pervasive SQL database. The manufacturer of the product using Pervasive tells me the version they are using is 10 which should support subqueries, but I have yet to be able to run even a simple subquery. So, I am wondering if the following query can be rewritten to eliminate the subquery:
SELECT
OuterTime.Employee,
OuterTime.Date,
OuterTime.Pay_ID,
OuterTime.Description,
OuterTime.Equipment,
OuterTime.JC_Cost_Code,
OuterTime.JC_Category,
OuterTime.Units,
(
SELECT
SUM(SubQueryTime.Units)
FROM
PRT_NEW__TIME AS SubQueryTime
WHERE
SubQueryTime.Employee = OuterTime.Employee
GROUP BY
SubQueryTime.Employee
) AS TotalHoursForEmp
FROM
PRT_NEW__TIME AS OuterTime
In standard SQL you can do this, though I have no idea about PervasiveSQL specifically...
SELECT
OuterTime.Employee,
OuterTime.Date,
OuterTime.Pay_ID,
OuterTime.Description,
OuterTime.Equipment,
OuterTime.JC_Cost_Code,
OuterTime.JC_Category,
OuterTime.Units,
COALESCE(TotalHoursForEmp.TotalUnits, 0) AS TotalUnits
FROM
PRT_NEW__TIME AS OuterTime
LEFT JOIN
(
SELECT
Employee,
SUM(Units) AS TotalUnits
FROM
PRT_NEW__TIME
GROUP BY
Employee
)
AS TotalHoursForEmp
ON TotalHoursForEmp.Employee = OuterTime.Employee
Not a Pervasive SQL guy, but would this work? Not sure it accomplishes your query goals:
SELECT
OuterTime.Employee,
OuterTime.Date,
OuterTime.Pay_ID,
OuterTime.Description,
OuterTime.Equipment,
OuterTime.JC_Cost_Code,
OuterTime.JC_Category,
SUM(OuterTime.Units) AS TotalHoursForEmp
GROUP BY OuterTime.Employee, OuterTime.Date, OuterTime.Pay_ID,
OuterTime.Description, OuterTime.Equipment, OuterTime.JC_Cost_Code,
OuterTime.JC_Category
FROM
PRT_NEW__TIME AS OuterTime

SQL Group By question SQL Server 2005 CE

I'm having a problem with an sql query that i use for my mobile application which uses sql server 2005 ce. I'm not so good with t-sql, so have a problem with this query
SELECT TP.ID_TASK_MASTER, TP.ID_PROBLEM, TP.ID_TASK_PROBLE, P.DS_PROBLEM,
TP.SW_HASOK, TP.SW_HASNOK, TP.SW_HASTOK, TP.SW_HASVALUE,
TP.NO_VALUE1, TP.NO_VALUE2
FROM TASK_PROBLEMS TP
INNER JOIN PROBLEMS P
ON TP.ID_PROBLEM = P.ID_PROBLEM
GROUP BY P.DS_PROBLEM,TP.ID_TASK_MASTER, TP.ID_PROBLEM, TP.ID_TASK_PROBLE
HAVING TP.ID_TASK_MASTER = #P_IDTASKMASTER
What i try to do is doing a group by on ds_problem field, getting an error like this :
{"In aggregate and grouping expressions, the SELECT clause can contain only aggregates and grouping expressions. [ Select clause = TP,SW_HASOK ]"}
So what i made wrong? Thanks..
I suppose what you meant to do was
SELECT TP.ID_TASK_MASTER, TP.ID_PROBLEM, TP.ID_TASK_PROBLE, P.DS_PROBLEM,
TP.SW_HASOK, TP.SW_HASNOK, TP.SW_HASTOK, TP.SW_HASVALUE,
TP.NO_VALUE1, TP.NO_VALUE2
FROM TASK_PROBLEMS TP
INNER JOIN PROBLEMS P
ON TP.ID_PROBLEM = P.ID_PROBLEM
WHERE TP.ID_TASK_MASTER = #P_IDTASKMASTER
ORDER BY P.DS_PROBLEM,TP.ID_TASK_MASTER, TP.ID_PROBLEM,TP.ID_TASK_PROBLE