how reproduce MySQL Statement in linq - c#

I have this MySQL query which does what I want. But I don't know how to translate this query to linq, the part of the UNION is what confuses me.
The MYSQL query:
SELECT * FROM conta.subrecurso as a
left join conta.recurso as b on a.idRecurso=b.idRecurso
left join conta.eventorecurso as c on b.idRecurso=c.idRecurso
left join conta.recursocliente as d on a.idSubrecurso=d.idSubrecurso
left join conta.eventocliente as e on d.idVenta=e.idVenta
where c.idEvento=47 And e.idVenta =784
UNION
SELECT * FROM conta.subrecurso as a
left join conta.recurso as b on a.idRecurso=b.idRecurso
left join conta.eventorecurso as c on b.idRecurso=c.idRecurso
left join conta.recursocliente as d on a.idSubrecurso=d.idSubrecurso
left join conta.eventocliente as e on d.idVenta=e.idVenta
WHERE c.idEvento=47 and e.idVenta is null ;

There's a similar question.
In the answer the solved it in that way:
(sqlstatement1).Union(sqlstatement2);

Related

Inner join and left join with query inside - from SQL to LINQ

I have prepared a SQL query and I'm having problem translating into LINQ query. Below I'm attaching my SQL query.
I have problems with inner join and left join that contains a select inside, and actually a second left join has another select inside a select as well, with group by.
This is really frustrating so maybe you could help me?
select * from Users u
inner join Pickups p on p.Number=u.Number
inner join Revisions r on r.Id=p.RevisionId and r.RevisionText='Done'
inner join (select u2.Id, count(u2.Id) clicks_num
from Users u2
inner join Clicks uc on uc.UserId=u2.Id
group by u2.Id) clicks on clicks.Id=u.Id
left join (select UserId, count(Id) scc
from (select distinct ucx.UserId, ucx.Id
from Clicks ucx
inner join Pickups ucpx on ucpx.Number=ucx.Number
inner join Revisions ucprx on ucprx.Id=ucpx.RevisionId and r.RevisionText='Done') t
group by UserId) s on s.UserId = u.Id
where clicks.clicks_num = s.scc;
I am starting with a simple one, but then could you give me an example how to make an inner select as join?
from u in Context.Set<Users>()
join p in Context.Set<Pickups>() on u.Number equals p.Number
join r in Context.Set<Revisions>() on p.RevisionId equals r.RevisionId

SQL to LINQ - Left Join Before Inner Join

So I have a SQL query that I would like to convert to LINQ.
Here is said query:
SELECT *
FROM DatabaseA.SchemaA.TableA ta
LEFT OUTER JOIN DatabaseA.SchemaA.TableB tb
ON tb.ShipId = ta.ShipId
INNER JOIN DatabaseA.SchemaA.TableC tc
ON tc.PostageId= tb.PostageId
WHERE tc.PostageCode = 'Package'
AND ta.MailId = 'Specification'
The problem I am struggling with is I cannot seem to figure out how to do a left join in LINQ before an inner join, since doing a left join in LINQ is not as clear to me at least.
I have found numerous examples of a LINQ inner join and then a left join, but not left join and then inner join.
If it helps, here is the LINQ query I have been playing around with:
var query = from m in tableA
join s in tableB on m.ShipId equals s.ShipId into queryDetails
from qd in queryDetails.DefaultIfEmpty()
join p in tableC on qd.PostageId equals p.PostageId
where m.MailId == "Specification" && p.PostageCode == "Package"
select m.MailId;
I have tried this a few different ways but I keep getting an "Object reference not set to an instance of an object" error on qd.PostageId.
LINQ is very new to me and I love learning it, so any help on this would be much appreciated. Thanks!
From my SQL conversion recipe:
JOIN conditions that aren't all equality tests with AND must be handled using where clauses outside the join, or with cross product (from ... from ...) and then where
JOIN conditions that are multiple ANDed equality tests between the two tables should be translated into anonymous objects
LEFT JOIN is simulated by using into joinvariable and doing another from from the joinvariable followed by .DefaultIfEmpty().
The order of JOIN clauses doesn't change how you translate them:
var ans = from ta in TableA
join tb in TableB on ta.ShipId equals tb.ShipId into tbj
from tb in tbj.DefaultIfEmpty()
join tc in TableC on tb.PostageId equals tc.PostageId
where tc.PostageCode == "Package" && ta.MailId == "Specification"
select new { ta, tb, tc };
However, because the LEFT JOIN is executed before the INNER JOIN and then the NULL PostageIds in TableB for unmatched rows will never match any row in TableC, it becomes equivalent to an INNER JOIN as well, which translates as:
var ans2 = from ta in tableA
join tb in tableB on ta.ShipId equals tb.ShipId
join tc in tableC on tb.PostageId equals tc.PostageId
where tc.PostageCode == "Package" && ta.MailId == "Specification"
select new { ta, tb, tc };
Use:
var query = from m in tableA
join s in tableB on m.ShipId equals s.ShipId
join p in tableC on s.PostageId equals p.PostageId
where m.MailId == "Specification" && p.PostageCode == "Package"
select m.MailId;
Your query uses a LEFT OUTER JOIN but it doesn't need it.
It will, in practice, function as an INNER JOIN due to your tc.PostageCode = 'Package' clause. If you compare to a column value in a table in a WHERE clause (and there are no OR clauses and you aren't comparing to NULL) then effectively all joins to get to that table will be treated as INNER).
That clause will never be true if TableB is null (which is why you use LEFT OUTER JOIN vs INNER JOIN) - so you should just use an INNER JOIN to make the problem simpler.

Many outer join with SQL to LINQ

Having a hard time with trying to rewrite an SQL query to Linq with many outer joins.
This is the query:
select on1.diskpath as d1,
on2.diskpath as d2,
of1.diskpath as d3,
of2.diskpath as d4,
on1.disknaam as n1,
on2.disknaam as n2,
of1.disknaam as n3,
of2.disknaam as n4
from tblstoragelocation
left join tblstoragedisks on1 on online1=on1.id
left join tblstoragedisks on2 on online2=on2.id
left join tblstoragedisks of1 on offline1=of1.id
left join tblstoragedisks of2 on offline2=of2.id where md5='xxx'";
I tried many things, this is one of them but it gives many errors: (loc is already declared, type inference failed in groupjoin, online2 could not be found)
from loc in fdc.tblStoragelocations
join _on1 in fdc.tblStoragedisks on loc.online1 equals _on1.id into on1
from _on2 in fdc.tblStoragedisks on loc.online2 equals _on2.id into on2
So how to write multiple left outer joins to LINQ?
Left outer join is achieved by .DefaultIfEmpty() method.
var q =
from iter_1 in collection_1
join iter_2 in collection_2 on iter_1 equals iter_2 into join_1
from iter_2 in join_1.DefaultIfEmpty()
join iter_3 in collection_3 on iter_2 equals iter_3 into join_2
from iter_3 in join_2.DefaultIfEmpty()
...
join iter_n in collection_n on iter_n_1 equals iter_n into join_n_1
from iter_n in join_n_1.DefaultIfEmpty()
select join_n_1;

NHibernate, Left outer join

How can I create nhibernate query, that should look like the below sql query
select * from A
left outer join B on A.ID = B.ID
left outer join C on B.ProdID = C.ProdID
Unfortunately, I can't use the named query.
And what the mapping of A should look like?
Thanks.
Map B as a many-to-one property of A. The B to C relation doesn't seem to be a normal foreign key and can't be mapped as a property. So it can't be left outer joined.
HQL, C can't be left outer joined:
select *
from A a
left join a.B b,
C c
where
c.Description = b.Description

SQL to Linq: RIGHT JOIN in LINQ

anybody can help me to convert some sql query whit right join like this to linq ?
SELECT dbo.FinnTrans.SanadID, dbo.FinnTrans.Date, dbo.FinnAccount.ID AS AccID,
dbo.FinnAccount.FullId, dbo.FinnAccount.Name, SUM(dbo.FinnTrans.Debit) AS TotalDebit,
SUM(dbo.FinnTrans.Credit) AS TotalCredit
FROM dbo.FinnAccount AS FinnAccount_1 LEFT OUTER JOIN
dbo.FinnAccount ON FinnAccount_1.ParentId = dbo.FinnAccount.ID RIGHT OUTER JOIN
dbo.FinnTrans LEFT OUTER JOIN
dbo.FinnAccount AS FinnAccount_2 ON dbo.FinnTrans.AccID = FinnAccount_2.ID ON
FinnAccount_1.ID = FinnAccount_2.ParentId
WHERE (dbo.FinnTrans.FPID = 7) AND (FinnAccount_2.AccLevel = 3)
GROUP BY dbo.FinnTrans.SanadID, dbo.FinnTrans.Date, dbo.FinnAccount.ID,
dbo.FinnAccount.Name, dbo.FinnAccount.FullId
HAVING (dbo.FinnTrans.SanadID = 1)
You can look here: http://www.hookedonlinq.com/OuterJoinSample.ashx as an example of the left outer join. And you can always swap tables to get either left or right
I've taken the liberty of beatifying your TSQL a little.
The last two join conditions appear malformed to me so this TSQL can not be parsed.
SELECT
[t].SanadID
, [t].Date
, [a].ID [AccID]
, [a].FullId
, [a].Name
, SUM([t].Debit) [TotalDebit]
, SUM([t].Credit) [TotalCredit]
FROM
dbo.FinnAccount [a1]
LEFT OUTER JOIN
dbo.FinnAccount [a]
ON [a1].ParentId = [a].ID
RIGHT OUTER JOIN
dbo.FinnTrans [t]
LEFT OUTER JOIN
dbo.FinnAccount [a2]
ON [a].AccID = [a2].ID
ON [a1].ID = [a2].ParentId
WHERE
[t].FPID = 7
AND
[a2].AccLevel = 3
GROUP BY
[t].SanadID
, [t].Date
, [a].ID
, [a].Name
, [a].FullId
HAVING
[t].SanadID = 1

Categories

Resources