Linq query with 3 tables - c#

Can you please let me know how to write a LINQ from for the following SQL query,
Select cr.Id
from [dbo].[User] usr, [dbo].[LikesStaging] lk, [dbo].[ChangeRequestStaging] cr
where usr.CustomerId=lk.[LikedBy] and usr.[Id] = 'user' and lk.[ChangeRequestId] = cr.[Id]
Was trying with the following query, but was not able to add usr.[Id] = 'user' condition in my linq query.
var result = from usr in lstUser
join lk in lstLikeStaging
on usr.CustomerId equals lk.LikedBy
join cr in lstChangeRequests
on lk.ChangeRequestId equals cr.Id
select new
{
cr.Id
};
Please let me know how to add this condition here.

var result = from usr in lstUser
join lk in lstLikeStaging on usr.CustomerId equals lk.LikedBy
join cr in lstChangeRequests on lk.ChangeRequestId equals cr.Id
where usr.id=="user"
select new
{
cr.Id
};

var result = from usr in lstUser
where usr.Id == "user"
join lk in lstLikeStaging
on usr.CustomerId equals lk.LikedBy
join cr in lstChangeRequests
on lk.ChangeRequestId equals cr.Id
select new
{
cr.Id
};

Related

Complex joins in Linq with multiple tables and LEFT OUTER JOIN

Hoping someone can point me in the right direction with this join. I'm trying to convert some SQL to Linq. My SQL has a left outer join after several inner joins. The following SQL produces the desired result:
SELECT TOP(50) [t].[TagFriendlyName] AS [TagName], [t0].[timeStamp] AS [LastSeen], [l].[Name] AS [LocationName]
FROM [Tags] AS [t]
INNER JOIN [tag_reads] AS [t0] ON [t].[epc] = [t0].[epc]
INNER JOIN [ReaderData] AS [r] ON [t0].[ReaderDataId] = [r].[Id]
LEFT OUTER JOIN [Readers] AS [r0] ON [r].[mac_address] = [r0].[mac_address]
INNER JOIN [Locations] AS [l] on [t0].[antennaPort] = [l].[AntennaId] AND [r].[Id] = [l].[ReaderId]
GROUP BY [t].[TagFriendlyName], [t0].[timeStamp], [l].[Name]
ORDER BY [t0].[timeStamp] DESC
My Linq code is as follows, but I can't figure out how to get the left outer join inserted properly. Not sure how to introduce the Readers table that needs the LEFT OUTER JOIN:
var query = (
from tags in db.Tags
join tagreads in db.tag_reads on tags.epc equals tagreads.epc
join readerdata in db.ReaderData on tagreads.ReaderDataId equals readerdata.Id
join readers in db.Readers on readerdata.mac_address equals readers.mac_address
group tags by new { tags.TagFriendlyName, timestamp = tagreads.timeStamp, readerdata.mac_address } into grp
select new CurrentStatus()
{
TagName = grp.Key.TagFriendlyName,
LastSeen = grp.Key.timestamp,
LocationName = grp.Key.mac_address
}
)
.OrderByDescending(o => o.LastSeen)
According to the documentation I need to use DefaultIfEmpty(), but I'm not sure where to introduce the Readers table.
Using EF Core 3.1.0. THANKS!
You should apply Left Join this way:
join readers in db.Readers on readerdata.mac_address equals readers.mac_address into readersJ
from readers in readersJ.DefaultIfEmpty()
The full code:
var query = (
from tags in db.Tags
join tagreads in db.tag_reads on tags.epc equals tagreads.epc
join readerdata in db.ReaderData on tagreads.ReaderDataId equals readerdata.Id
join readers in db.Readers on readerdata.mac_address equals readers.mac_address into readersJ
from readers in readersJ.DefaultIfEmpty()
join locations in db.Locations
on new { ap = tagreads.antennaPort, rd = readerdata.Id }
equals new { ap = locations.AntennaId, rd = locations.ReaderId }
group tags by new { tags.TagFriendlyName, timestamp = tagreads.timeStamp, readerdata.mac_address } into grp
select new CurrentStatus()
{
TagName = grp.Key.TagFriendlyName,
LastSeen = grp.Key.timestamp,
LocationName = grp.Key.mac_address
}
)
.OrderByDescending(o => o.LastSeen)

Linq join with newest record of second table

I have two related tables Ticket and Status.
Every ticket can be multiple status. (Open, assigned, closed). But I want all tickets but only one status (newest date) to show.
I could handle with this query on t-sql;
SELECT d.ticketID, statusName, c.statusDate, c.assignedTo,c.statusID
FROM Ticket d LEFT JOIN Status c ON c.ticketID = d.ticketID
WHERE c.statusID = (
SELECT MAX(statusID)
FROM Status c2
WHERE c2.ticketID = d.ticketID)
This is my Linq :
var result = from t in db.Ticket
join s in db.Status.OrderByDescending(x=>x.statusDate).Take(1)
on t.ticketID equals s.ticketID join c in db.Customer
on t.customerID equals c.customerID
But this only returning one row.
I solve my problem with linqer application via converting my Tsql query to linq
from s in db.Status
where s.statusID ==
(from c2 in db.Status where
c2.ticketID == s.Ticket.ticketID &&
c2.statusName == "New" ||
c2.statusName == "Assigned"
select new
{
c2.statusID
}).Max(p => p.statusID)
Thanks for everyone.
Try changing your original linq logic to:
var result = from t in db.Ticket
join s in db.Status on t.ticketID equals s.ticketID into sGroup
from s in sGroup.OrderByDescending(x=>x.statusDate).Take(1)
join c in db.Customer
on t.customerID equals c.customerID

SQL Outer Join in LINQ

I've got the following LINQ Query
from s in db.tblSave
join a in db.tblAssessment on s.AssessmentID equals a.Id
join staff in db.tblStaff on s.StaffID equals staff.Id
join student in db.tblStudent on s.StudentID equals student.Id
join signed in db.tblSaveSigned on s.Id equals signed.SaveID
select new
{
SaveID = s.Id,
StaffName = staff.StaffName,
AssessmentName = a.AssessmentName,
StudentName= student.StudentName,
CreatedDate = s.CreatedDate,
SignedDate = signed.SignedDate}
tblSaveSigned may not have a record against tblSave which is excluding some tblSave records. How do I include all tblSave records in my results? It's an outer join in SQL but not sure how to do it in LINQ.
Thanks in advance.
Is there an SQL to LINQ tool?
Below is the correct LINQ Query for put FULL OUTER JOIN on tblSave for tblSaveSigned. Thanks for NetMage for pointing me in the right direction.
from s in db.tblSave
join a in db.tblAssessment on s.AssessmentID equals a.Id
join staff in db.tblStaff on s.StaffID equals staff.Id
join student in db.tblStudent on s.StudentID equals student.Id
join signed in db.tblSaveSigned on s.Id equals signed.SaveID into signedj
from signed in signedj.DefaultIfEmpty()
select new
{
SaveID = s.Id,
StaffName = staff.StaffName,
AssessmentName = a.AssessmentName,
StudentName= student.StudentName,
CreatedDate = s.CreatedDate,
SignedDate = signed.SignedDate}

Sql server query equivalent in LINQ

I am trying to convert the following query in LINQ, tried different links but no luck so far. Please help with below:
SELECT T.TASKID,
T.TITLE,
T.DESCRIPTION,
T.DEADLINE,
T.CREATEDON,
(SELECT EMAIL FROM ASPNETUSERS WHERE ID = T.CREATEDBYUSERID) AS INITIATEDBY
FROM TBL_TASKMEMBERS AS M
INNER JOIN TBL_TASKS AS T ON M.TASKID = T.TASKID
INNER JOIN ASPNETUSERS AS U ON M.USERID = U.ID
WHERE m.UserId = '95d2f49c-0ae6-4571-9d7b-1c498ad0bfac'
Thanks in advance !
Try this:
var result =
from member in TBL_TASKMEMBERS
join task in TBL_TASKS on member.TASKID equals task.TASKID
join user in ASPNETUSERS on user.ID equals member.USERID
join usermail in ASPNETUSERS on usermail.ID equals task.CREATEDBYUSERID
where member.UserId = '95d2f49c-0ae6-4571-9d7b-1c498ad0bfac'
select new { TASKID = task.TASKID, TITLE = task.TITLE, DESCRIPTION = taks.DESCRIPTION, DEADLINE = task.DEADLINE, CREATEDON = task.CREATEDON, INITIATEDBY = usermail.EMAIL };

Outer join in LINQ Problem

Id like to perform an outer join with the second join statement in this query, I keep getting weird errors! (it must be the 3rd RedBull)
var Objeto = from t in Table1.All()
join su in table2.All() on t.Id equals su.Id
join tab2 in Table1.All() on t.PId equals tab2.Id //<-I want it here
select new
{
t.Field1,
SN = su.Field123,
PTN = tab2.FieldABC
};
Any help would be appreciated.
[Edit] - I neglected to say that I'm using SubSonic 3.0, the bug seems to be with SubSonic.....
Performing an outer join requires two steps:
Convert the join into a group join with into
Use DefaultIfEmpty() on the group to generate the null value you expect if the joined result set is empty.
You will also need to add a null check to your select.
var Objeto = from t in Table1.All()
join su in table2.All() on t.Id equals su.Id
join tab2 in Table1.All() on t.PId equals tab2.Id into gj
from j in gj.DefaultIfEmpty()
select new
{
t.Field1,
SN = su.Field123,
PTN = (j == null ? null : j.FieldABC)
};

Categories

Resources