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

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

Related

Multiple JOIN in Linq-to-SQL

I'm trying to do the following query in linq-to-sql (joining 3 different tables):
select * from tbl_round r
inner join tbl_election e on r.fk_election_id = e.election_id
inner join tbl_meeting m on m.meeting_id = e.fk_meeting_id
Here is what I have so far but not correct:
from round in db.tbl_rounds
join meeting in db.tbl_meetings on election.fk_meeting_id equals meeting.meeting_id
join election in db.tbl_elections on round.fk_election_id equals election.election_id
select round;
The error I'm getting is that the name 'election' does not exist in the current context.
You will have to re-order the join statement probably like
from round in db.tbl_rounds
join election in db.tbl_elections on round.fk_election_id equals election.election_id
join meeting in db.tbl_meetings on election.fk_meeting_id equals meeting.meeting_id
select round;
Because you have "election" used before it is declared.
from round in db.tbl_rounds
join meeting in db.tbl_meetings on -->election<--.fk_meeting_id equals meeting.meeting_id
join -->election<-- in db.tbl_elections on round.fk_election_id equals election.election_id
select round;
In this case, you will need to change order in your query.
Query should look like this:
from round in db.tbl_rounds
join election in db.tbl_elections on round.fk_election_id equals election.election_id
join meeting in db.tbl_meetings on election.fk_meeting_id equals meeting.meeting_id
select round;

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.

LINQ syntax for SQL query with multiple inner joins and aliases

I am working on creating a C# LINQ statement from a SQL query with multiple joins and aliases. I am having some trouble constructing the LINQ.
The SQL:
SELECT
store.Name as 'Store',
store.CreatedOn as 'StoreCreated',
supplier.Name as 'Supplier',
supplier.CreatedOn as 'SupplierCreated',
farm.Name as 'Farm',
farm.CreatedOn as 'FarmCreated',
FROM Users store
INNER JOIN UserRelationship toSupplier on store.ID = toSupplier.YId
INNER JOIN Users supplier ON supplier.ID = toSupplier.XId
INNER JOIN UserRelationship toFarm ON store.ID = toFarm.XId
INNER JOIN Users farm ON farm.ID = toFarm.YId
WHERE
store.Active= '1'
AND
supplier.Active = '1'
AND
farm.Active = '1'
This returns rows showing the relationships between the three parties and the dates.
So, far, I've got the following LINQ:
var newUserList = from store in Users
join toSupplier in UserRelationship on store.ID = toSupplier.YId
join supplier in Users on supplier.ID = toSupplier.XId
join toFarm in UserRelationship on store.ID = toFarm.XId
join farm in Users on farm.ID = toFarm.YId
Am I on the right track? Any help would be appreciated.
LINQ join syntax uses equals keyword instead of = in join condition:
var newUserList = from store in Users
join toSupplier in UserRelationship on store.ID equals toSupplier.YId
join supplier in Users on supplier.ID equals toSupplier.XId
join toFarm in UserRelationship on store.ID equals toFarm.XId
join farm in Users on farm.ID equals toFarm.YId
select ...

LEFT Join not returning records in MS Access

My recent project I have a requirements to print receivable summary. I need to return entire rows from the OpeningBalance table and matching rows from VoucherHeader and Customers.
My SQL query is this
SELECT
OpeningBalance.OpenID, Sum(OpeningBalance.Amount) AS SumOfAmount,
Sum(VoucherHeader.Debit) AS SumOfDebit, Sum(VoucherHeader.Credit) AS SumOfCredit,
Customers.CustomerID, Customers.CustomerName
FROM
(OpeningBalance
LEFT OUTER JOIN
VoucherHeader ON OpeningBalance.OpenID = VoucherHeader.LedgerID)
INNER JOIN
Customers ON OpeningBalance.OpenID = Customers.CustomerID
WHERE
(((Customers.CustomerType)='Debtor')
AND ((VoucherHeader.VoucherDate)<#2013/06/02#))
GROUP BY
OpeningBalance.OpenID, Customers.CustomerID, Customers.CustomerName,
VoucherHeader.LedgerID
ORDER BY
Customers.CustomerName;
Please help.
SELECT OB.OpenID, Sum(OB.Amount) AS SumOfAmount,
Sum(VB.Debit) AS SumOfDebit, Sum(VB.Credit) AS SumOfCredit, CS.CustomerID,
CS.CustomerName FROM OpeningBalance OB
LEFT OUTER JOIN VoucherHeader VB ON OB.OpenID = VB.LedgerID
LEFT OUTER JOIN Customers CS ON OB.OpenID = CS.CustomerID
WHERE (((CS.CustomerType)='Debtor') AND ((VB.VoucherDate)<#2013/06/02#))
GROUP BY OB.OpenID, CS.CustomerID, CS.CustomerName, VB.LedgerID
ORDER BY CS.CustomerName;

nHibernate complex join

I have a need to perform a complex left join on a table and am unsure how to write the code using a criteria query. Currently I have:
public IList<RezolutionConfig> GetSearchConfigByManagerCategoryProduct(int ManagerId, int ProductTypeId, int ConfigCategoryId)
{
ICriteria criteria = Session.GetISession().CreateCriteria(typeof(RezolutionConfig))
.CreateAlias("RezolutionConfigCategory", "rcc")
.Add(Expression.Eq("rcc.id", ConfigCategoryId))
.CreateAlias("RezolutionProductType","rpt")
.Add(Expression.Eq("rpt.id", ProductTypeId))
.CreateAlias("RezolutionManagerConfigs", "rmc", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.CreateAlias("rmc.Manager", "m", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.Add(Expression.Eq("m.id", ManagerId));
return criteria.List<RezolutionConfig>();
}
which produces:
SELECT *
FROM [dbo].[RezolutionConfig] this_
inner join [dbo].[RezolutionConfigCategory] rcc1_ on this_.[RezolutionConfigCategoryId]=rcc1_.[RezolutionConfigCategoryId]
inner join [dbo].[RezolutionProductType] rpt2_ on this_.[RezolutionProductTypeId]=rpt2_.[RezolutionProductTypeId]
left outer join [dbo].[RezolutionManagerConfig] rmc3_ on this_.[RezolutionConfigID]=rmc3_.[RezolutionConfigID]
left outer join [dbo].[Manager] m4_ on rmc3_.[ManagerID]=m4_.[ManagerID] WHERE rcc1_.[RezolutionConfigCategoryId] = 1
and rpt2_.[RezolutionProductTypeId] = 1
and m4_.[ManagerID] = 9135
What I need to produce is
SELECT *
FROM [dbo].[RezolutionConfig] this_
inner join [dbo].[RezolutionConfigCategory] rcc1_ on this_.[RezolutionConfigCategoryId]=rcc1_.[RezolutionConfigCategoryId]
inner join [dbo].[RezolutionProductType] rpt2_ on this_.[RezolutionProductTypeId]=rpt2_.[RezolutionProductTypeId]
left outer join [dbo].[RezolutionManagerConfig] rmc3_ on this_.[RezolutionConfigID]=rmc3_.[RezolutionConfigID]
left outer join [dbo].[Manager] m4_ on rmc3_.[ManagerID]=m4_.[ManagerID] and m4_.[ManagerID] = 9135
WHERE rcc1_.[RezolutionConfigCategoryId] = 1
and rpt2_.[RezolutionProductTypeId] = 1
NHibernate doesn't support adding a constraint to a join as you would like: left outer join [dbo].[Manager] m4_ on rmc3_.[ManagerID]=m4_.[ManagerID] and m4_.[ManagerID] = 9135. I think your best approach will be to write the query in HQL and use a subquery to constrain Manager.

Categories

Resources