How to translate an SQL query to LINQ - c#

How to translate an SQL query to LINQ
SELECT DISTINCT q.Id
FROM Questions AS q
JOIN TagsQuestions AS tq
ON q.Id = tq.QuestionForeignKey
JOIN Tags AS t
ON t.Id = tq.TagForeignKey
WHERE t.Name IN ('C#', '.Net')
GROUP BY q.Id
HAVING COUNT(*) = 2

Firstly, You should note use DISTINCT q.Id because it's already GROUP BY q.Id.
SELECT q.Id
FROM Questions AS q
JOIN TagsQuestions AS tq ON q.Id = tq.QuestionForeignKey
JOIN Tags AS t ON t.Id = tq.TagForeignKey
WHERE t.Name IN ('C#', '.Net')
GROUP BY q.Id
HAVING COUNT(*) = 2
Secondly, You can transfer from sql to linq syntax like below
var TagNames = new[] {
"C#",
".Net"
};
var result = from q in Questions
join tq in TagsQuestions on q.Id equals tq.QuestionForeignKey
join t in Tags on t.Id equals tq.TagForeignKey
where TagNames.Contains(t.Name)
group q by q.Id into g
where g.Count() = 2
select g.Key;

Related

Complicated SQL query to Linq

I need to convert the following complicated sql query to Linq in C#:
Select Empleador.NombreComercial as Empresa,
Vacante.Puesto as Vacante,
Vacante.Actividades,
COUNT(Vacante.CveVacante) as Visitas
from Vacante
LEFT JOIN Empleador on Empleador.CveEmpleador=Vacante.CveEmpleador
LEFT JOIN VisitaVacante on Vacante.CveVacante = VisitaVacante.CveVacante
GROUP BY Empleador.NombreComercial,Vacante.Puesto, Vacante.Actividades,
Vacante.CveVacante HAVING COUNT(*) > 1 ORDER BY Visitas DESC
For the moment I already have the following:
var Visitas = (from tvacante in db.VacanteT
join tEmpleador in db.EmpleadorT on tvacante.CveEmpleador equals tEmpleador.CveEmpleador
join tVisitaVacante in db.VisitaVacanteT on tvacante.CveVacante equals tVisitaVacante.CveVacante
select new
{
Empresa = tEmpleador.NombreComercial,
Vacante = tvacante.Puesto,
tvacante.Actividades,
Visitas = tvacante.CveVacante
}).GroupBy( );
How can I add the COUNT(Vacante.CveVacante) as Visitas and also the
GROUP BY Empleador.NombreComercial,Vacante.Puesto, Vacante.Actividades,
Vacante.CveVacante HAVING COUNT(*) > 1 ORDER BY Visitas DESC
to my linq query? I can't find information about how to complete this. The tables are tvacante, templeador, and tvisitaVacante.
Try this:
var Visitas =(from tvacante in db.VacanteT
join tEmpleador in db.EmpleadorT on tvacante.CveEmpleador equals tEmpleador.CveEmpleador
join tVisitaVacante in db.VisitaVacanteT on tvacante.CveVacante equals tVisitaVacante.CveVacante
group new{tEmpleador.NombreComercial,tvacante.Puesto, tvacante.Actividades} by new {tEmpleador.NombreComercial,tvacante.Puesto, tvacante.Actividades} into g
where g.Count()>1
select new
{
Empresa = g.Key.tEmpleador.NombreComercial,
Vacante = g.Key.tvacante.Puesto,
Actividades= g.Key.tvacante.Actividades,
Visitas = g.Count()
}).OrderByDescending(e=>e.Visitas);
If you want to do it using only linq query syntax and not merging both syntax then you could also do this:
var Visitas = from tvacante in db.VacanteT
join tEmpleador in db.EmpleadorT on tvacante.CveEmpleador equals tEmpleador.CveEmpleador
join tVisitaVacante in db.VisitaVacanteT on tvacante.CveVacante equals tVisitaVacante.CveVacante
group new{tEmpleador.NombreComercial,tvacante.Puesto, tvacante.Actividades} by new {tEmpleador.NombreComercial,tvacante.Puesto, tvacante.Actividades} into g
where g.Count()>1
orderby g.Count() descending
select new
{
Empresa = g.Key.tEmpleador.NombreComercial,
Vacante = g.Key.tvacante.Puesto,
Actividades= g.Key.tvacante.Actividades,
Visitas = g.Count()
};

left join linq query in FF4

I have the following SQL Query:
SELECT DISTINCT *
FROM Documents d
LEFT JOIN
Invoices
ON (i.invoicedocumentid = d.id or i.jobsheetdocumentid = d.id)
INNER JOIN
PurchaseOrders PO ON i.poid = PO.id
LEFT JOIN
HelpDeskFaults f
ON f.id = PO.helpDeskFaultId
LEFT JOIN stores s
ON s.id = f.storeid
WHERE s.name = 'Linden Drive'
OR d.id in (
SELECT u.id as 'docid'
FROM documents u
INNER JOIN stores s
ON u.storeid = s.id
WHERE s.name = 'Linden Drive'
)
ORDER BY d.Id
So far my linq query looks like this:
var documents = from doc in context.Documents
from invoice in context.Invoices
join po in context.PurchaseOrders on invoice.PurchaseOrder.PurchaseOrderId equals po.PurchaseOrderId
join hdf in context.HelpDeskFaults on po.HelpdeskFaultId equals hdf.ID into hdfpo
from hs in hdfpo.DefaultIfEmpty()
join store in context.Stores on hs.StoreID equals store.ID into hsstore
from hss in hsstore.DefaultIfEmpty()
where hss.Name.Contains(jobSearchParams.StoreName) && (invoice.InvoiceDocumentId == doc.ID || invoice.JobSheetInvoiceId == doc.ID)
select doc;
But the sql that is produced is nothing like what I expected.
Can anyone give me any pointers of how to improve my linq query.
This is what I would translate your SQL query into
var results =
(from doc in context.Documents
from invoice in (
from inv in context.Invoices
where inv.InvoiceDocumentId == doc.ID || inv.JobSheetInvoiceId == doc.ID
select inv).DefaultIfEmpty()
from hs in invoice.PurchaseOrder.HelpDeskFaults.DefaultIfEmpty()
from hss in hs.Stores.DefaultIfEmpty()
where hss.Name == "Linden Drive" || doc.Store.Name == "Linden Drive"
order by doc.ID
select new
{
Document = doc,
Invoice = invoice,
invoice.PurchaseOrder,
HelpDeskFault = hs,
Store = hss,
}).Distinct();
I included that anonymous class of the 5 entities because your SQL is using Select *. I would recommend changing it to only return the minimum required set of fields.

Convert SQL into LINQ and Lambda

Please is somebody able to help me convert the following SQL query into LINQ
select p.Description,SUM(s.TotalArea) as TotalArea from Stands s
inner join ContractProducts cp on s.Id = cp.StandId
inner join Products p on cp.ProductId = p.Id
where s.EventId = 1
group by p.Description
Thanks in advance
Maybe something like this:
var result= (
from s in db.Stands
join cp in db.ContractProducts
on s.Id equals cp.StandId
join p in db.Products
on cp.ProductId equals p.Id
where s.EventId == 1
group p by p.Description into g
select new
{
Description=g.Key,
TotalArea = g.Sum (x =>x.TotalArea)
}
).ToList();
Where db is the linqdatacontext

convert this into linq or lambda c#

Can someone please help me convert this SQL to linq or lambda c#
select
count(s.ClassId) [StudentInClass], c.Name [Class], t.Name [teacher]
from
[dbo].[Students] s
inner join
class c on s.ClassId = c.Id
inner join Teacher t
on t.Id = c.TeacherId
group by
s.ClassId, c.Name, t.Name
so far this is what I have, and i am messing it up. I want to achieve the same results as in my sql query
SchoolEntities db = new SchoolEntities();
var StudentsByCourseId = from s in db.Students
join c in db.Classes on s.ClassId equals c.Id
join t in db.Teachers on c.TeacherId equals t.Id
group c by s.ClassId
into g
select g;
in SQL this is what my reults look like, it counts the students in a class by the teacher
StudentCount Class Teacher
1 Geography Teacher1
1 Biology Teacher1
2 Maths Teacher2
You can use an anonymous class to group by multiple properties.
var StudentsByCourseId = from s in db.Students
join c in db.Classes on s.ClassId equals c.Id
join t in db.Teachers on c.TeacherId equals t.Id
group s by new { s.ClassId, Class = c.Name, Teacher = t.Name }
into g
select new
{
StudentInClass = g.Count(),
g.Key.Class,
g.Key.Teacher,
};

How to Implement this Sql block contain (Having , Join and Group By) with Linq

my sql statement is
SELECT c.type,c.title,c.datereg, d.ranknum
FROM T_News AS c
INNER JOIN (
SELECT a.id, COUNT(*) AS ranknum
FROM T_News AS a
INNER JOIN T_News AS b
ON (a.type = b.type)
AND (a.datereg >= b.datereg)
GROUP BY a.id
HAVING COUNT(*) <= 3
) AS d ON (c.id = d.id)
ORDER BY c.type, d.ranknum
that i get http://rickosborne.org/blog/2008/01/sql-getting-top-n-rows-for-a-grouped-query/
for Getting TOP N rows for a grouped query
EFUnitOfWork EF = new EFUnitOfWork();
T_NewsRepository News = new T_NewsRepository();
News.UnitOfWork = EF;
var query =
from news1 in News.All()
join news2 in News.All()
on news1.type equals news2.type into resjoin
group news1 by news1.id into idgroup
where idgroup.Count() <= 3
select new { idgroup };
var x = query.ToList();
I did not get any error , but "where idgroup.Count() <= 3" did not work and i get all rows in db as result
Break it down into it's smallest components and then compose the larger query from that. Let's start with the innermost query that makes sense:
SELECT
a.id, COUNT(*) AS ranknum
FROM
T_News AS a
INNER JOIN T_News AS b ON
(a.type = b.type) AND
(a.datereg >= b.datereg)
GROUP BY
a.id
HAVING
COUNT(*) <= 3
I'd convert this to:
// Items with counts/ranknum
var ranknum =
from a in News.All()
join b in News.All() on
a.type equals b.type
where
a.datereg > b.datereg
group by a.id into g
select new { g.Key as id, g.Count() as ranknum };
// Filter the ranknum.
ranknum = ranknum.Where(rn => rn.ranknum <= 3);
Then joining that with the outer query:
SELECT
c.type,c.title,c.datereg, d.ranknum
FROM
T_News AS c
INNER JOIN (<sub-query from above>) as d ON
c.id = d.id
ORDER BY
c.type, d.ranknum
That part becomes simple, as it's just a join between two existing queries.
var query =
from c in News.All()
join rn in ranknum on c.id = rn.id
orderby c.type, rn.ranknum
select new { c.type, c.title, c.datereg, rn.ranknum };
Chances are the SQL that LINQ-to-Entities generates for this is going to look really ugly, and probably be inefficient, in which case, you might want to consider placing this logic in a stored procedure and then calling that through LINQ-to-Entities (which is generally true for more complex queries).

Categories