Change to Left joins - linq

How can I change this Linq query to left joins instead inner joins?
from EH in db.EventsHistory
join AH in db.EventsAttr on EH.TRANSACTIONID equals AH.TRANSACTIONID
join E in db.Events on EH.EVTID equals E.EVTID
group EH by new { EH.TRANSACTIONID, EH.MACGRP, EH.MACID, EH.ORDID, EH.ORDSPLIT, EH.LINID, EH.EVTDATETIME, EH.MATID, EH.PRODID, E.DESC, NUM_ATTR = AH.TRANSACTIONID } into grouped
select new { grouped.Key.TRANSACTIONID, grouped.Key.MACGRP, grouped.Key.MACID, grouped.Key.ORDID, grouped.Key.ORDSPLIT, grouped.Key.LINID, grouped.Key.EVTDATETIME, grouped.Key.MATID, grouped.Key.PRODID, grouped.Key.DESC, NUM_ATTR = grouped.Count() };

In Linq LEFT JOIN is created by using group join with method DefaultIfEmpty. The basic construction looks like this using query syntax:
var query = from c in db.Customers
join o in db.Orders on c.Id equals o.CustomerId into g
from o in g.DefaultIfEmpty()
select new
{
Customer = c,
Order = o
};
Example with method syntax:
var query = db.Customers
.GroupJoin(db.Orders, c => c.Id, o => o.CustomerId, (c, og) => new
{
Customer = c,
Order = og.DefaultIfEmpty()
});
So your query should look like this:
var query = from eh in db.EventsHistory
join ah in db.EventsAttr on eh.TransactionId equals ah.TransactionId into g
join e in db.Events on eh.EvtId equals e.EvtId into g2
from ah in g.DefaultIfEmpty()
from e in g2.DefaultIfEmpty()
group eh by new { ... } into grouped
select new { ... }

Related

Linq - unable to join table due to null value

I am having problem with joining of multiple tables.
PatientID can be null. Whenever patientID is null, it is unable to join and the result will not be added to the list.
Any suggestion on how i can do to join them ?
List<ViewActivityLogViewModel> q = new List<ViewActivityLogViewModel>();
q = (from l in logs
join lc in logCategories on l.logCategoryID equals lc.logCategoryID
join p in patients on l.patientID equals p.patientID
join u in users on l.userIDInit equals u.userID
join ut in usertypes on u.userTypeID equals ut.userTypeID
join u2 in users on l.userIDApproved equals u2.userID
join ut2 in usertypes on u2.userTypeID equals ut2.userTypeID
select new ViewActivityLogViewModel
{
logID = l.logID,
logCategoryName =lc.logCategoryName,
firstName = p.firstName,
userIDInitfirstName = u.firstName,
userIDInituserTypeName = ut.userTypeName,
userIDApprovedfirstName = u2.firstName,
userIDApproveduserTypeName = ut2.userTypeName
}
).OrderBy(x=>x.createDateTime).ToList();
This is the answer i need. I written it in SQL.
SELECT l.logID, lc.logCategoryName,p.firstName, p.lastName, u.firstName, ut.userTypeName, u2.firstName, ut2.userTypeName
FROM log AS l
LEFT JOIN patient AS p ON l.patientID = p.patientID
JOIN logCategory AS lc ON l.logCategoryID = lc.logCategoryID
JOIN user AS u ON l.userIDInit = u.userID
JOIN userType AS ut ON u.userTypeID = ut.userTypeID
JOIN user AS u2 ON l.userIDApproved = u2.userID
JOIN userType AS ut2 ON u2.userTypeID = ut2.userTypeID
You can use GroupJoin to join the patients table and get the data. According to MSDN, GroupJoin in Lambda supports the LEFT JOIN in SQL.
GroupJoin preserves the order of the elements of outer, and for each
element of outer, the order of the matching elements from inner.
GroupJoin has no direct equivalent in
traditional relational database terms. However, this method does
implement a superset of inner joins and left outer joins. Both of
these operations can be written in terms of a grouped join.
So, your query can be written as,
var results =
logs.Join(logCategories, l => l.logCategoryID, lc => lc.logCategoryID, (l, lc) => new { l, lc })
.Join(users, llc => new { userId = llc.l.userIDInit }, u1 => new { userId = u1.userID }, (llc, u1) => new { llc.l, llc.lc, u1 })
.Join(userTypes, llcu1 => llcu1.u1.userTypeID, ut1 => ut1.userTypeID, (llcu1, ut1) => new { llcu1.l, llcu1.lc, llcu1.u1, ut1 })
.Join(users, llcu2 => new { approvedId = llcu2.l.userIDApproved }, u2 => new { approvedId = u2.userID }, (llcu2, u2) => new { llcu2.l, llcu2.lc, llcu2.u1, llcu2.ut1, u2 })
.Join(userTypes, llcu3 => llcu3.u2.userTypeID, ut2 => ut2.userTypeID, (llcu3, ut2) => new { llcu3.l, llcu3.lc, llcu3.u1, llcu3.u2, llcu3.ut1, ut2 })
.GroupJoin(patients, llcu4 => llcu4.l.patientID, p => p.patientID, (llcu4, p) => new { llcu4.l, llcu4.lc, llcu4.u1, llcu4.u2, llcu4.ut1, llcu4.ut2, p })
.SelectMany(x => x.p.DefaultIfEmpty(), (prj, y) =>
{
return new ViewActivityLogViewModel
{
logID = prj.l.logID,
logCategoryName = prj.lc.logCategoryName,
firstName = y != null ? y.firstName : null,
userIDInitfirstName = prj.u1.firstName,
userIDInituserTypeName = prj.ut1.userTypeName,
userIDApprovedfirstName = prj.u2.firstName,
userIDApproveduserTypeName = prj.ut2.userTypeName
};
}).ToList();
C# Fiddle with sample data.
I am not sure why did u
replicated the joins which could be achieved in two joins only
JOIN user AS u ON l.userIDInit = u.userID
JOIN userType AS ut ON u.userTypeID = ut.userTypeID
JOIN user AS u2 ON l.userIDApproved = u2.userID
JOIN userType AS ut2 ON u2.userTypeID = ut2.userTypeID
as
JOIN user AS u ON l.userIDInit = u.userID and l.userIDApproved = u.userID
JOIN userType AS ut ON u.userTypeID = ut.userTypeID
The below query will give u ur desired result
q = (from l in log
join lc in logCategory
on l.logCategoryID equals lc.logCategoryID
join u in user
on new {a1=l.userIDInit,a2=l.userIDApproved} equals
new {a1=u.userID, a2=u.userID}
join ut in userType
on u.userTypeID equals ut.userTypeID
join p in patient
on l.patientID equals p.patientID
into p11
from p1 in p11.DefaultIfEmpty()
select new {l,lc,u,ut,p1})
.Select(x=>new ViewActivityLogViewModel
{
logID = x.l.logID,
logCategoryName =x.lc.logCategoryName,
firstName = x.p1?.firstName,
userIDInitfirstName = x.u.firstName,
userIDInituserTypeName = x.ut.userTypeName,
userIDApprovedfirstName = x.u.firstName,
userIDApproveduserTypeName = x.ut.userTypeName
})
.OrderBy(x=>x.createDateTime).ToList();
You didn't say which PatientId can be null, so to be on the safe side I assume both can be null.
q = (from l in logs
join lc in logCategories.Where(category => category.PatientId != null)
on l.logCategoryID equals lc.logCategoryID
join p in patients where(patient => patien.PatiendId != null)
on l.patientID equals p.patientID
join ...

Need help converting SQL into LINQ

SELECT ra.ResidentID, ra.RoomID, r.Number, ra.StartDate, p.FacilityID
FROM(
SELECT ResidentID, MAX(StartDate) AS max_start
FROM RoomAssignments
GROUP BY ResidentID
) m
INNER JOIN RoomAssignments ra
ON ra.ResidentID = m.ResidentID
AND ra.StartDate = m.max_start
INNER JOIN Rooms r
ON r.ID = ra.RoomID
INNER JOIN Person p
ON p.ID = ra.ResidentID
inner join ComplianceStage cs
ON cs.Id = p.ComplianceStageID
ORDER BY ra.EndDate DESC
I'm trying to figure out how to convert this to C# using LINQ. I'm brand new with C# and LINQ and can't get my subquery to fire correctly. Any chance one of you wizards can turn the lights on for me?
Update-----------------
I think I've got the jist of it, but am having trouble querying for the max startdate:
var maxQuery =
from mra in RoomAssignments
group mra by mra.ResidentID
select new { mra.ResidentID, mra.StartDate.Max() };
from ra in RoomAssignments
join r in Rooms on ra.RoomID equals r.ID
join p in Persons on ra.ResidentID equals p.ID
where ra.ResidentID == maxQuery.ResidentID
where ra.StartDate == maxQuery.StartDate
orderby ra.ResidentID, ra.StartDate descending
select new {ra.ResidentID, ra.RoomID, r.Number, ra.StartDate, p.FacilityID}
Following my LINQ to SQL Recipe, the conversion is pretty straight forward if you just follow the SQL. The only tricky part is joining the range variable from the subquery for max start date to a new anonymous object from RoomAssignments that matches the field names.
var maxQuery = from mra in RoomAssignments
group mra by mra.ResidentID into mrag
select new { ResidentID = mrag.Key, MaxStart = mrag.Max(mra => mra.StartDate) };
var ans = from m in maxQuery
join ra in RoomAssignments on m equals new { ra.ResidentID, MaxStart = ra.StartDate }
join r in Rooms on ra.RoomID equals r.ID
join p in Persons on ra.ResidentID equals p.ID
join cs in ComplianceStage on p.ComplianceStageID equals cs.Id
orderby ra.EndDate descending
select new {
ra.ResidentID,
ra.RoomID,
r.Number,
ra.StartDate,
p.FacilityID
};

LINQ left outer join NullReferenceException

I am getting NullReferenceException when doing left outer join.
Similar query works in LINQ Pad but not in Visual Studio 2015. System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Thanks in advance.
.Net Core LINQ Code that does not work:
public IEnumerable<R> GetAll()
{
var results =
from a in uow.R.GetAll()
join b in uow.SSR.GetAll() on a.ID equals b.RID into g9
from c in g9.DefaultIfEmpty()
join d in uow.SS.GetAll() on c.SSID equals d.ID into g10
from e in g10.DefaultIfEmpty()
select new
{
a.Id,
SSID = c == null ? null : (int?)c.SSID,
e.Name
};
List<R> rList = new List<R>();
foreach (var a in results)
{
resourceList.Add(new R { ID = a.Id, SSID = a.SSID });
}
return rList.ToArray<R>();
}
`
var Result =
from a in Tbl_R
join b in Tbl_SS on a.ID equals b.RID into g9
from c in g9.DefaultIfEmpty()
join d in Lu_SS on c.SSID equals d.SSID into g10
from e in g10.DefaultIfEmpty()
select new
{
a.ID,
SSID = c == null ? null : (int?)c.SSID,
e.Name
};
Result.Dump();
SELECT t2.ID,
ISNULL(t17.SSID,'') as SSID
ISNULL(t17.Name,'') as Name
FROM R t2
LEFT JOIN SSR t16 ON t16.RID=t2.ID
LEFT JOIN SS t17 ON t16.SSID=t17.SSID

linq to entities left outer join

select SF.FOLDER_NAME,SF.CREATED_DATE,COUNT(st.FOLDER_ID)
from SURVEY_FOLDER SF with (nolock) left outer join SURVEY_TEMPLATE ST with (nolock)
on SF.FOLDER_ID=ST.FOLDER_ID
group by SF.FOLDER_NAME,SF.CREATED_DATE
I need this query in Linq :
I have tried this query,but unable to group by.
My Linq Query :
var data = (from xx in VDC.SURVEY_FOLDER
join yy in VDC.SURVEY_TEMPLATE
on xx.FOLDER_ID equals yy.FOLDER_ID into g
from grt in g.DefaultIfEmpty()
select
new
{
xx.FOLDER_NAME,
xx.CREATED_DATE,
count = g.Count()
}).ToList();
I got the Answer :
var data5 = (from SF in VDC.SURVEY_FOLDER
join ST in VDC.SURVEY_TEMPLATE on new { FOLDER_ID = SF.FOLDER_ID } equals new { FOLDER_ID = (Int64)ST.FOLDER_ID } into ST_join
from ST in ST_join.DefaultIfEmpty()
group new { SF, ST } by new
{
SF.FOLDER_NAME,
SF.CREATED_DATE
} into g
select new
{
g.Key.FOLDER_NAME,
CREATED_DATE = (DateTime?)g.Key.CREATED_DATE,
Column1 = (Int64?)g.Count(p => p.ST.FOLDER_ID != null)
}).ToList();

Multiple conditions with Joins in Linq

For the below code it gives the error
type reference failed in the call to Join
how can to fix this
var unp = from v in context.student.Include("subjects").Include("marks")
join n in context.AllStudents
on v.StudentDetails.student_Id equals n.Id
join ss in context.StudentHistory
on v.StBacklogs_Id equals ss.Id
join userName in context.Users
on v.CreatedBy_Id equals userName.Id
join externalId in context.Books
on new { ss.BookNumber, ss.Id } equals new { externalId.BookNumber, externalId.Id }
select new { v, n, ss, userName,externalId };

Resources