LINQ LEFT JOIN to another LEFT JOIN - sql

I am pretty green with LINQ and brand new to converting LEFT JOINs.
I wrote a query in SQL (which works fine) and I am trying to get it into LINQ. I am missing something, but can't see it. The SQL is:
DECLARE #Version int = 1
SELECT Q.WebFormTemplateQuestionID, Lay.*
FROM WebFormTemplates WFT
INNER JOIN WebFormTemplateQuestions Q ON Q.WebFormTemplateID = WFT.WebFormTemplateID
LEFT JOIN WebFormTemplateLayoutHeaders Hdr ON Hdr.WebFormTemplateID = WFT.WebFormTemplateID and hdr.Version = #Version
LEFT JOIN WebFormTemplateLayouts Lay ON Lay.WebFormTemplateLayoutHeaderID = Hdr.WebFormTemplateLayoutHeaderID AND Lay.WebFormTemplateQuestionID = Q.WebFormTemplateQuestionID
WHERE WFT.WebFormTemplateID = 2
ORDER BY Q.SortOrder
The LINQ I wrote is:
Version = 1
Dim q3 = From WFT In ctx.WebFormTemplates
Join Q In ctx.WebFormTemplateQuestions On Q.WebFormTemplateID Equals WFT.WebFormTemplateID
Group Join Hdr In ctx.WebFormTemplateLayoutHeaders On Hdr.WebFormTemplateID Equals WFT.WebFormTemplateID Into Hdr_join = Group
From Hdr In Hdr_join.Where(Function(x) x.Version = Version).DefaultIfEmpty()
Group Join Lay In ctx.WebFormTemplateLayouts On Lay.WebFormTemplateLayoutHeaderID Equals Hdr.WebFormTemplateLayoutHeaderID And Lay.WebFormTemplateQuestionID Equals Q.WebFormTemplateQuestionID Into Lay_join = Group
From Lay In Lay_join.DefaultIfEmpty()
Where WFT.WebFormTemplateID = 2
With this, I can break the code and in the immediate window get the expected values when I type:
?q3.FirstOrDefault().Hdr.Version
But for the Lay item I need to type:
?q3.FirstOrDefault().Lay.FirstOrDefault().QuestionAlign
to get the value.
Why do I need to call FirstOrDefault the second time? What am I missing here?
When I do a Select, the values are all null for Lay I assume because I don't have another FirstOrDefault someplace. Everything from Hdr, WFT, and Q work just fine. Can anyone explain what is going on?

i have written your equivalent Linq query from the SQL in C#.
Update accordingly for VB.
Version = 1
Dim q3 = (from WFT In ctx.WebFormTemplates
join Q In ctx.WebFormTemplateQuestions on Q.WebFormTemplateID equals WFT.WebFormTemplateID
join Hdr In ctx.WebFormTemplateLayoutHeaders on Hdr.WebFormTemplateID equals WFT.WebFormTemplateID into Hdr_join
from Hdr In Hdr_join.Where(Function(x) x.Version = Version).DefaultIfEmpty()
join Lay In ctx.WebFormTemplateLayouts on new { Lay.WebFormTemplateLayoutHeaderID,Lay.WebFormTemplateQuestionID } equals new {Hdr.WebFormTemplateLayoutHeaderID, Q.WebFormTemplateQuestionID} into Lay_join
from Lay In Lay_join.DefaultIfEmpty()
where WFT.WebFormTemplateID = 2
select new {Q.WebFormTemplateQuestionID, Lay});

Related

VB.net linq left join where null or 0

I'm having a hard time figuring out how to do a left join and pull records where the join is null, or the join works, but the column paid is 0. This is the sql I'm trying to recreate
SELECT *
FROM JobsCompleted
LEFT JOIN SqWar ON JobsCompleted.JobFk = SqWar.JobFk
WHERE SqWar.SqWarId IS NULL OR SqWar.Paid = 0
This is what I have so far in linq
'not sure here how I do if the join is null, or joins, but paid is 0
Dim jobIds = (From jc In db.JobsCompleteds
Group Join sqw In db.SqWars On sqw.JobFk Equals jc.JobFk Into SqGroup = Group
Where jc.DateCompleted >= fromInput.SelectedDate And jc.DateCompleted <= toInput.SelectedDate And
SqGroup Is Nothing) 'not sure here how I do if it's null or paid = 0
With a Group Join, if you want a SQL LEFT JOIN, especially since you are wanting to test a field from the right hand side, you need to use a second From clause with DefaultIfEmpty:
Dim jobIds = From jc In db.JobsCompleteds
Group Join sqw In db.SqWars On sqw.JobFk Equals jc.JobFk Into Group
From sqw In Group.DefaultIfEmpty()
Where sqw Is Nothing Or sqw.Paid = 0
Select New With { jc, sqw }
Note that LINQ doesn't have a direct equivalent to SQL SELECT * so I returned an anonymous object with sub-objects. If you wanted the result to be flattened, you would have to explicitly list all fields.
If you wanted to process the join groups you could also filter each group by your criteria:
Dim jobIdsG = From jc In db.JobsCompleteds
Group Join sqw In db.SqWars On sqw.JobFk Equals jc.JobFk Into Group
Select New With {jc, .sqGroup = Group.Where(Function(sq) sq.Paid = 0)}

Why LINQ Count() returning multiple rows instead one?

I would like to translate the following SQL into LINQ:
select count(p.ID) as NumPosts,
count(t.Trustee_ID)as TrusteePost,
count(pat.ID)as PatientPost,
count(s.ID) as SpecialistPost
from [dbo].[Posts] as p
left join [dbo].[Trusteehips] as t
on p.Autor_ID = t.Trustee_ID
left join [dbo].[Patients] as pat
on p.Autor_ID = pat.ID
left join [dbo].[Specialists] as s
on p.Autor_ID = s.ID
where p.Deleted = 0
I I've tried this:
var res = from p in context.Posts
join t in context.Trusteeships
on p.Autor.ID equals t.Trustee.ID into tGroup
join pat in context.Patients
on p.Autor.ID equals pat.ID into patGroup
join s in context.Specialists
on p.Autor.ID equals s.ID into sGroup
select new NumUserPosts
{
//CountAllPosts = ?
TrusteePost = tGroup.Count(),
PatientPost = patGroup.Count(),
SpecialistPost = sGroup.Count()
};
But result is this:
1 0 0
0 0 1
0 0 1
0 1 0
and etc.
I expect result
TrusteePost PatientPost SpecialistPost
1000 2000 3000
Why when i try to count group return this result?
SQL query is correct. I would like to translate into LINQ.
The query returns 0 or 1 records per joined Trustee, etc. because you outer join by the unique primary key. So a join into (which is a GroupJoin in fluent syntax) produces a group of 0 or 1 records. If you run the generated SQL query and view the raw query result you'd probably understand better what's going on.
The problem is, there is no LINQ equivalent for count(t.Trustee_ID), etc. Therefore it's impossible to do what you want in one query without "hacking".
Hacking it into one query could be done like so:
(from p in context.Posts.Take(1)
select new
{
TrusteePost = context.Posts
.Count(p1 => context.Trusteeships.Any(x => x.ID == p1.Autor.ID)),
PatientPost = context.Posts
.Count(p2 => context.Patients.Any(x => x.ID == p2.Autor.ID)),
SpecialistPost = context.Posts
.Count(p3 => context.Specialists.Any(x => x.ID == p3.Autor.ID))
})
.AsEnumerable()
.Select(x => new NumUserPosts
{
CountAllPosts = x.TrusteePost + x.PatientPost + x.SpecialistPost,
x.TrusteePost,
x.PatientPost,
x.SpecialistPost
}
The SQL query will be much more elaborate than the original SQL (for example, it involves cross joins), but it will probably still perform pretty well. AsEnumerable prevents the second part from being executed as SQL, which would bloat the SQL statement even more. It simply runs in memory.
I consider this a hack because the first part, context.Posts.Take(1) doesn't really have any meaning, it's only there to serve as a wrapper for the three separate queries. It's poor man's query packaging.
It looks like you're doing group joins instead of left outer joins (see this page).
A left outer join looks more like:
var res = from p in context.Posts
join t in context.Trusteeships
on p.Autor.ID equals t.Trustee.ID into tGroup
from tJoin in tGroup.DefaultIfEmpty()
join pat in context.Patients
on p.Autor.ID equals pat.ID into patGroup
from patJoin in patGroup.DefaultIfEmpty()
join s in context.Specialists
on p.Autor.ID equals s.ID into sGroup
from sJoin in sGroup.DefaultIfEmpty()
select ...
Unfortunately, it doesn't seem that Linq can create a query to count elements in each column.
If you don't mind using multiple queries, you could count each separately, for example:
var trusteePost = (from p in context.Posts
join t in context.Trusteeships on p.Autor.ID equals t.Trustee.ID
select t).Count()

Translate from SQL to Linq

I'm really new with this whole Linq story, but I have a good understanding in SQL.
I need to rebuild a query from SQL to Linq. My SQL query is working perfectly and so far I have tried to do something by myself with Linq but without a good result...
Is it possible that someone could help me to translate this query from SQL to Linq?
I'm really ready to learn something new in this whole story. It would be nice if you could why is it working like that in linq.
SQL Statement
SELECT TimeReport.EntryDate
, SUM(TimeReport.Hours) AS Hours
, SUM(BillingRate.HourlyRate * TimeReport.Hours) AS Amount
FROM BillingRate
INNER JOIN Activity
ON BillingRate.BillingRateId = Activity.BillingRateIt
INNER JOIN TimeReport
ON Activity.ActivityId = TimeReport.ActivityId
RIGHT OUTER JOIN Dossier
ON TimeReport.DossierId = Dossier.DossierId
INNER JOIN LBU
ON Dossier.LBUId = LBU.LBUId
INNER JOIN BU
ON Dossier.BUId = BU.BUId
GROUP BY TimeReport.EntryDate
HAVING SUM(TimeReport.Hours) > 0
ORDER BY TimeReport.EntryDate desc
What I have tired with Linq
var x = (from br in ctx.BillingRate
join a in ctx.Activity on br.BillingRateId equals a.BillingRateIt
join tr in ctx.TimeReport on a.ActivityId equals tr.ActivityId
select br)
.GroupJoin(
(from d in ctx.Dossier
join l in ctx.LBU on d.LBUId equals l.LBUId
join b in ctx.BU on d.DossierId equals
Thanks for help and fast answer.
I appreciated every effort !!
Joins aren't necessary when you have navigation properties. Since I don't know exactly what your model looks like, use the following as a starting point and adjust to your own specifications:
// starting with Dossier handles the right outer join condition
var timeRecordQuery =
from d in ctx.Dossier
from tr in d.TimeReports
// this handles the inner join conditions after the right outer join
where d.LBUId.HasValue && d.BUId.HasValue
// project what you want to use
select new
{
EntryDate = tr.EntryDate,
Hours = tr.Hours,
// simply use navigation properties, no need to join
Amount = tr.Activity.BillingRate.HourlyRate * tr.Hours
};
var resultsQuery = from e in timeRecordQuery
// group by EntryDate
group e by e.EntryDate into g
// get sum of hours for each EntryDate value
let hours = g.Sum( x => x.Hours )
// having clause
where hours > 0
// project results
select new
{
EntryDate = g.Key,
Hours = hours,
Amount = g.Sum( x => x.Amount )
};

Group By Having and Count as LINQ query with multiply nested tables

I have the following SQL query to return all Customers who have no OrderLines with no Parts assigned - i.e. I only want the customers within which every order line of every order has no parts assigned - (in the actual problem I am dealing with a different domain but have translated to customers/orders to illustrate the problem)
SELECT c.Customer_PK
FROM Customers c
INNER JOIN Orders o
ON c.Customer_PK = o.Customer_FK
LEFT OUTER JOIN OrderLines l
ON o.Order_PK = l.Order_FK
LEFT OUTER JOIN Parts p
ON l.OrderLine_PK = p.OrderLine_FK
GROUP BY c.Customer_PK
HAVING COUNT(p.Part_PK) = 0
The best I have come up with in LINQ is as follows:
Dim qry =
(From c In context.Customers
Select New With { c.Customer_PK,
.CountParts =
(From o In c.Orders
From l In o.OrderLines
Select l.Parts.Count).DefaultIfEmpty.Sum})
qry = (From grp In qry
Where grp.CountParts = 0
Select grp.Customer_PK)
This works but generates less than optimal SQL - it is doing a subquery for Count on each row of the customers query rather than using Group By and Having. I tried making the LINQ Group By syntax work but it kept putting the filter as a WHERE not a HAVING clause.
Any ideas?
Edit in response to Answers below:
I am accepting JamieSee's answer as it addresses the stated problem, even though it does not produce the GROUP BY HAVING query I originally had.
Thanks Peter and Nick for your input on this. I am a VB developer so I have had a crack translating your code to VB, this is the closest I got to but it does not quite produce the desired output:
Dim qry = From c In context.Customers
Group Join o In context.Orders On c.Customer_PK Equals o.Customer_FK
Into joinedOrders = Group
From jo In joinedOrders.DefaultIfEmpty
Group Join l In context.OrderLines On jo.Order_PK Equals l.Order_FK
Into joinedLines = Group
From jl In joinedLines.DefaultIfEmpty
Group c By Key = New With {c.Customer_PK, jl} Into grp = Group
Where Key.jl Is Nothing OrElse Not Key.jl.Parts.Any
Select c.Customer_PK
The problem I had is that I have to push "jl" into the Group By "Key" so I can reference it from the Where clause, otherwise the compiler cannot see that variable or any of the other variables appearing before the Group By clause.
With the filter as specified I get all customers where at least one order has lines with no parts rather than only customers with no parts in any order.
Given that you don't care about the counts, only the resulting customers, consider the folllowing restatement of the problem:
Identify all Customers who do not have any Orders that have Lines with Parts.
This yields:
var customersWithoutParts = from c in Context.Customers
where !(from o in Context.Orders
from l in o.Lines
from p in l.Parts
select o.Customer_FK).Contains(c.Customer_PK)
select c.Customer_PK;
This should yield emitted SQL that is roughly equivalent to the following:
SELECT c.Customer_PK
FROM Customers AS c
WHERE (NOT EXISTS
(SELECT o.Cusomer_FK
FROM Orders AS o INNER JOIN
OrderLines AS l ON o.Order_PK = l.Order_FK INNER JOIN
Parts AS p ON l.OrderLine_PK = p.OrderLine_FK
WHERE (o.Customer_FK = c.Customer_PK)))
To get the SQL you were trying to reproduce, I'd start by trying the following:
var customersWithoutParts = from c in Context.Customers
from o in c.Orders.DefaultIfEmpty()
from l in o.Lines.DefaultIfEmpty()
join part in Context.Parts on part.OrderLine_FK equals l.OrderLine_PK into joinedParts
where joinedParts.Count() == 0
select c.Customer_PK;
Note that in VB the join here would be replaced by Group Join.
Just a tipp bocuse hard to create a query without the generated modells (C#):
from o in dc.Orders
join jOrderLines in dc.OrderLines on o.Order_PK equals jOrderLines.Order_FK into joinedOrderlines
from l in joinedOrderLines.DefaultIfEmpty()
group o by o.Customer_FK into g
where l == null || l.Count(x => x.Parts) == 0
select g.Key
What about something like this:
var qry = from c in db.Customers
join o in db.Orders.Where(x => x.Customer_FK == c.Customer_PK)
join l in db.OrderLines.Where(x => x.Order_FK = o.Order_PK).DefaultIfEmpty()
join p in db.Parts.Where(x => x.OrderLine_FK = l.OrderLine_PK).DefaultIfEmpty()
group c by new
{
c.Customer_PK
} into g
where g.Count(p => p.Part_PK != null) == 0
select new
{
Customer_PK = g.Key.Customer_PK
};

Convert advanced SQL query with nested joins to Linq-to-sql

I have ran into a snag with my Linq-to-Sql.
I have a sql query that runs the way I want and usually I use Linqer to convert to Linq to see the general idea. But this time my SQL query seems to advanced for Linqer. :/
I think the problem is the INNER JOINS that are nested in the LEFT OUTER JOIN. Unfortunately I have never ran into this before and don't know how to solve it using Linq.
My SQL query looks like this:
SELECT c.[Company], c.[Name_First], c.[Name_Last], ort.[IDOriginatorRoleType],
ort.[RoleType] AS [OriginatorRoleType], o.[IDOriginator], o.[IDWork],
o.[IDContact], m.[IDMedia], m.[IDWork], m.[FileName], m.[FileNameOnDisk],
m.[DateAdded], w.[IDWork] AS [IDWork2], w.[ArticleNumber], w.[Title],
w.[FrontPageLow], w.[FrontPageLowOnDisk], w.[FrontPageHigh],
w.[FrontPageHighOnDisk]
FROM [dbo].[tblSubscriptionsWorks] AS sw
INNER JOIN [dbo].[tblWorks] AS w ON sw.[IDWork] = w.[IDWork]
LEFT OUTER JOIN [dbo].[tblMedias] AS m ON m.[IDWork] = w.[IDWork]
LEFT OUTER JOIN ([dbo].[tblOriginators] AS o
INNER JOIN [dbo].[tblOriginatorRoles] AS ors ON
o.[IDOriginatorRole] = ors.[IDOriginatorRole]
INNER JOIN [dbo].[tblOriginatorRoleTypes] AS ort ON
ors.[IDOriginatorRoleType] = ort.[IDOriginatorRoleType]
INNER JOIN [dbo].[tblContacts] AS c ON
o.[IDContact] = c.[IDContact]) ON
(o.[IDWork] = w.[IDWork]) AND (ort.[IDOriginatorRoleType] = 1)
WHERE sw.[IDWork_Subscription] = 9942
The left outer join is not a problem what I can see. You just have to divide the statement
LEFT OUTER JOIN ([dbo].[tblOriginators] AS o
INNER JOIN [dbo].[tblOriginatorRoles] AS ors ON
o.[IDOriginatorRole] = ors.[IDOriginatorRole]
INNER JOIN [dbo].[tblOriginatorRoleTypes] AS ort ON
ors.[IDOriginatorRoleType] = ort.[IDOriginatorRoleType]
INNER JOIN [dbo].[tblContacts] AS c ON
o.[IDContact] = c.[IDContact]) ON
(o.[IDWork] = w.[IDWork]) AND (ort.[IDOriginatorRoleType] = 1)
into another IQueryable list. In the example the variable db is the datacontext. Here is a suggestion to a solution:
//selects all the columns that is just in the select from the left join
var leftJoin=
(
from o in db.tblOriginators
join ors in db.tblOriginatorRoles
on o.IDOriginatorRole equals ors.IDOriginatorRole
join ort in db.tblOriginatorRoleTypes
on ors.IDOriginatorRoleType equals ort.IDOriginatorRoleType
join c in db.tblContacts
on o.IDContact equals c.IDContact
where ort.IDOriginatorRoleType==1
select new
{
o.IDWork,
c.Company,
c.Name_First,
c.Name_Last,
ort.IDOriginatorRoleType,
ort.RoleType,
o.IDOriginator,
o.IDContact
}
);
var output=(
from sw in db.tblSubscriptionsWorks
join w in db.tblWorks
on sw.IDWork equals w.IDWork
from m in db.tblMedias
.Where(x=>x.IDWork==w.IDWork).DefaultIfEmpty()
//Left join with the IQueryable list
from org in leftJoin
.Where(x =>x.IDWork==w.IDWork).DefaultIfEmpty()
where
sw.IDWork_Subscription == 9942
select new
{
org.Company,
org.Name_First,
org.Name_Last,
org.IDOriginatorRoleType,
OriginatorRoleType=org.RoleType,
org.IDOriginator,
org.IDWork,
m.IDMedia,
m.IDWork,
m.FileName,
m.FileNameOnDisk,
w.FrontPageLow,
w.FrontPageLowOnDisk,
w.FrontPageHigh,
w.FrontPageHighOnDisk
}
);