I'll try to make it short: I'm new to LINQ. So there.
I have three tables, that i have EntityFrameworked into my solution:
Employees Desks Rooms
-------------- ------------ ------------
FirstName DeskId RoomId
DeskId RoomId RoomName
Color HasWindows
Width
I need a view with:
Employee Room name
----------------------------------
Lumberg Corner Office
Milton Storage in basement
Peter Cubicle 214
So, I created an EmployeeRoomListViewModel, and fill it using JOINs in my LINQ. No sweat, except
I'm missing some records. Namely those Employees without an assigned desk (DeskId is null). I need those also.
Here's my LINQ:
from Emp in db.Employees
join D in db.Desks on D.DeskId equals Emp.DeksId
join R in db.Rooms on D.RoomId equals R.RoomId
select new EmployeeRoomListViewModel
{
Employee = Emp.FirstName,
Room = R.RoomName
}).ToList();
Any ideas?
Your query is correct but you just need to add "Into Alias" and DefaultIfEmpty() in them.
from Emp in db.Employees
join Des in db.Desks on Des.DeskId equals Emp.DeksId into SomeThing
from Des in SomeThing.DefaultIfEmpty();
you need to use DefaultIfEmpty clause for Left Outer Join
from Emp in db.Employees
join D in db.Desks on D.DeskId equals Emp.DeksId into employeeDesks
from ed in employeeDesks.DefaultIfEmpty()
join R in db.Rooms on D.RoomId equals R.RoomId into deskRooms
from dr in deskRooms.DefaultIfEmpty()
select new EmployeeRoomListViewModel
{
Employee = Emp.FirstName,
Room = R.RoomName
}).ToList();
This will solve your problem
from Emp in db.Employees
join D in db.Desks on D.DeskId equals Emp.DeksId into desklist from deskjoin in
desklist.DefaultIfEmpty()
join R in db.Rooms on deskjoin.RoomId equals R.RoomId into roomlist from roomjoin in roomlist.DefaultIfEmpty() select new EmployeeRoomListViewModel
{
Employee = Emp.FirstName,
Room = roomjoin.RoomName
}).ToList();
first time you need to take left join employees table with Desks table and then you need to take left join with result of employees and Desk table join
In this "into desklist from deskjoin in desklist.DefaultIfEmpty()" is used for left join query
Related
I have two tables, Customers And ListTypeData. I just want to join these two tables.
I have CityId And DistrictId In Customers table.
It gives me both City and District same how to fix
for City And District.
SELECT c.[CustomerId]
,c.[Name]
,c.[CompanyName]
,c.[ShopNo]
,list.[Description] AS 'City'
,list.[Description] AS 'District'
FROM [MakkiRuskFaisalabad].[dbo].[Customers] c
JOIN [dbo].[ListTypesData] list ON c.CityId = list.ListTypeDataId ]
You need to join to the ListTypesData table a second time:
SELECT c.CustomerId
,c.Name
,c.CompanyName
,c.ShopNo
,list.Description AS 'City'
,list2.Description AS 'District'
FROM MakkiRuskFaisalabad.dbo.Customers c
JOIN dbo.ListTypesData list ON c.CityId = list.ListTypeDataId
JOIN dbo.ListTypesData list2 ON c.DistrictId = list2.ListTypeDataId
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;
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}
I have struggling to run linq left on multiple tables.
tableA
Select all the (courseID, code, title) from courseInstances table
tableB
select (studyLevel_ID) from Courses table where courseID from tableA = CourseID from tableB. tableB has courseID
tableC
Select (StudyLevelDescription) from StudyLevel table where studyLevelID from tableB = studyLevel from tableC.
I believe I need left join on table A as I need all the records
I have done separate linq which are working fine but struggling to bring combine result
CourseInstances results
var processedCourseInstance =
(from _courseInstances in _uof.CourseInstances_Repository.GetAll()
join _courses in _uof.Courses_Repository.GetAll() on _courseInstances.CourseID equals _courses.CourseID
into a from b in a.DefaultIfEmpty()
orderby _courseInstances.CourseCode
select _courseInstances).ToList();
StudyLevel results for each course
var _CoursesStudyLevel_Lookup =
(from _course in _uof.Courses_Repository.GetAll()
join _studyLevel in _uof.StudyLevel_Repository.GetAll() on _course.StudyLevelId equals _studyLevel.StudyLevelID
select new {_course.CourseID, _course.StudyLevelId, _studyLevel.LevelDescription }).ToList();
I have managed to combine two results but NOT with LEFT join on CourseInstance table. This time I used LINQPad
from _courseInstances in CourseInstances
join _courses in Courses on _courseInstances.CourseID equals _courses.CourseID
join _studylevel in StudyLevels on _courses.StudyLevelId equals _studylevel.StudyLevelID
orderby _courseInstances.CourseCode
select new {_courseInstances.CourseID, _courseInstances.CourseCode, _courseInstances.CourseTitle, _courseInstances.UCASCode, _courses.StudyLevelId, _studylevel.LevelDescription, _studylevel.SLevelType }
for above SQL version as following;
SELECT [t0].[CourseID], [t0].[CourseCode], [t0].[CourseTitle],
[t0].[UCASCode], [t1].[StudyLevelId], [t2].[LevelDescription], [t2].[SLevelType]
FROM [CourseInstances] AS [t0]
INNER JOIN [Courses] AS [t1] ON [t0].[CourseID] = ([t1].[CourseID])
INNER JOIN [StudyLevel] AS [t2] ON [t1].[StudyLevelId] = ([t2].[StudyLevelID])
ORDER BY [t0].[CourseCode]
If i understand correctly, you want something like this?
from _courseInstances in CourseInstances
join c in Courses on _courseInstances.CourseID equals c.CourseID into courses
from _courses in courses.DefaultIfEmpty()
join sl in StudyLevels on _courses.StudyLevelId equals sl.StudyLevelID into studyLevels
from _studylevel in studyLevels.DefaultIfEmtpy()
orderby _courseInstances.CourseCode
select new {
_courseInstances.CourseID,
_courseInstances.CourseCode,
_courseInstances.CourseTitle,
_courseInstances.UCASCode,
_courses.StudyLevelId,
_studylevel.LevelDescription,
_studylevel.SLevelType
}
}
You can create a LINQ left join query with the into keyword and .DefaultIfEmpt().
I have three tables below:
table patient
{ NID *pk
Name
Family
}
table disease
{
ICD_code *pk
Title
}
table_patient_disease *-*
{
ID
Fk_ICDcode
FK_Patient
}
it is a n-m relationship between patient and disease.
i want to write a query to select patients and their diseases
it needs to join in linq
the Sql pure query is:
SELECT
dbo.Table_Disease.*,
dbo.Table_PatDis.*,
dbo.Table_Patient.*
FROM
dbo.Table_Disease
INNER JOIN dbo.Table_PatDis ON dbo.Table_Disease.ICD_code = dbo.Table_PatDis.FK_Disease
INNER JOIN dbo.Table_Patient ON dbo.Table_PatDis.FK_PAtient = dbo.Table_Patient.NID
what is the same Linq statement ?
SELECT
dbo.Table_Disease.*,
dbo.Table_PatDis.*,
dbo.Table_Patient.*
FROM
dbo.Table_Disease
INNER JOIN dbo.Table_PatDis ON dbo.Table_Disease.ICD_code = dbo.Table_PatDis.FK_Disease
INNER JOIN dbo.Table_Patient ON dbo.Table_PatDis.FK_PAtient = dbo.Table_Patient.NID
Would become:
var results = (from d in DbContext.Table_Disease
join pd in DbContext.Table_PatDis on d.ICD_Code equals pd.FK_Disease
join p in DbContext.Table_Patient on pd.FK_PAtient equals p.NID
select new {d, pd, p});