How to translate SQL to LINQ? - linq

sql
select * from (select * from tabla limit 1) as a inner join table b on a.id = b.id where a.id = id
LINQ
from s in tableA
join c in (from a in tableB where a.id==id select a).FirstOrDefault() on s.id equals c.id
where s.id == id
select s
I want to translate this sql to linq but failed. How can I do translate?

I think this will do it, but you should check what sql is being generated by linq.
a = tableA.FirstOrDefault();
//First option
tableB.Where(xb => xb.id = a.id)
.Select(xb => new {a = a, b = xb})
.Where(abx => abx.a.id == id)
//Second option
tableB.Select(xb => new {a = a, b = xb})
.Where(abx => abx.a.id == id && abx.a.id = abx.b.id)

Related

How do I convert this query into LINQ?

SELECT *
FROM
ProcedureLookup ProcLookup
INNER JOIN (SELECT PatientProcedures.PP_ProcedureId, COUNT(*) as ProcCount
FROM (PatientProcedures INNER JOIN Treatments ON PatientProcedures.PP_TreatmentId = Treatments.TS_TreatmentId)
WHERE YEAR(Treatments.TS_Date) = YEAR(GETDATE())
GROUP BY PatientProcedures.PP_ProcedureId) cyearProc ON ProcLookup.PL_ProcedureId = cyearProc.PP_ProcedureId
ORDER BY ProcCount DESC;
Here ProcedureLookup, Treatments, PatientProcedures are the tables.
Here linq query.
var result = (from ProcLookup in db.ProcedureLookup
join cyearProc in (
from p in db.PatientProcedures
join t in db.Treatments on p.PP_TreatmentId equals
t.TS_TreatmentId
where
t.TS_Date.Year == DateTime.Now.Year
group p by p.PP_ProcedureId into g
select new
{
PP_ProcedureId = g.Key,
ProcCount = g.Count()
}
) on ProcLookup.PL_ProcedureId equals
cyearProc.PP_ProcedureId
orderby cyearProc.ProcCount descending
select new
{
// Columns
PP_ProcedureId = ProcLookup.PP_ProcedureId,
ProcCount = cyearProc.ProcCount
}).ToList();

convert sql to linq with left outer join

I have a sql having left outer join, I want to convert it to linq. the sql is
Select P.Surname, P.Othername H.history
from customers P left outer join historyfiles H on H.patID = P.patId
and H.category1 = 4
where P.id = 2299
Thank you very much.
You can try this:
var query = from p in db.customers.Where(t => t.id == 2299 )
join h in db.historyfiles.Where(l => l.category1 == 4 ) on p.patID equals h.patID into gj
from h in gj.DefaultIfEmpty()
select new { p.Surname, p.Othername, h.History };

Left join to a SET using linq

In SQL, I would commonly inner join two or more tables to create a set and then left join to that set:
select *
from TableA
left outer join
TableB
inner join TableC on TableB.Id = TableC.TableBId
on TableA.Id = TableB.TableAId
What is the Linq equivalent of this? I'm using EF Code First.
Like this:
var result = from a in TableA
join b in TableB on a.Id equals b.TableAId into ab
from b in ab.DefaultIfEmpty()
join c in TableC on b.Id equals c.TableBId
select a;
I was able to achieve this by breaking it into two statements. First, make your inner join set, then left join to that set.
var bc = from b in TableB
join c in TableC on b.Id equals c.TableBId
select new { B = b, C = c };
var result = from a in TableA
from bcRow in bc.Where(row => a.Id equals row.B.TableAId).DefaultIfEmpty()
select new { A = a, B = bcRow.B, C = bcRow.C };

Linq left outer join and group by issue

Hi All my following query returns results like this:
lstView.DataSource = (from h in context.HolidayMains
join hd in context.HolidayDetails on h.Id equals hd.HolidayMainId into hd2
from hd in hd2.DefaultIfEmpty()
join e in context.Employees on h.CreatedBy equals e.Id into e2
from e in e2.DefaultIfEmpty()
join o in context.OfficeLocations on hd.OfficeLocation equals o.Id into o2
from o in o2.DefaultIfEmpty()
select new
{
h.HolidayTitle,
h.Date,
OfficeLocation = o.Name,
CreatedBy = e.Name,
h.Id
}).ToList();
But I need result like this :
How it can be done?

multiple tables join by Linq

I have 3 tables which named Player, PlayerDetails and Team. I want to write a linq statement such as
from p in Player join d in PlayerDetails on p.ID equals d.PID
and then right join team table, Player has a column named TID which face to Team table's ID.
I have tried to write a statement like
from p in Player join d in PlayerDetails on p.ID equals d.PID into PlayerGroup
from t in team join g in PlayerGroup on t.ID equals g.p.ID
It certainly can't work. I'm not sure how to write such type of query statement, table left join table2 then right join table3.
Who can help?
I believe you could do something like this:
var LeftJoin =
from p in Player
join d in PlayerDetails on p.ID equals d.PID into pd
from d in pd.DefaultIfEmpty()
select new
{
pID = p.ID,
pTID = p.TID,
dID = d.ID
};
var RightJoin =
from t in Team
join l in LeftJoin on t.ID equals l.pTID into tl
from l in tl.DefaultIfEmpty()
select new
{
tID = t.ID,
pID = l.pID,
pTID = l.PTID,
dID = l.dID
};
To do everything in one query, I think you could do (not tested) something like this:
var RightJoin =
from t in Team
join l in
(from p in Player
join d in PlayerDetails on p.ID equals d.PID into pd
from d in pd.DefaultIfEmpty()
select new
{
pID = p.ID,
pTID = p.TID,
dID = d.ID
})
on t.ID equals l.pTID into tl
from l in tl.DefaultIfEmpty()
select new
{
tID = t.ID,
pID = l.pID,
pTID = l.PTID,
dID = l.dID
};

Resources