How To Fix Join Problem Show Same Results - c#

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

Related

How can i join 1 id fields from a table to 2 different table colums

I'm making a school menagement program in which i want to connect multiple tables to the Students table, i want to use the Cities ID field for both of the Stundents BirthPlace and their actual Address in the table but i can't figure it out how to do it.
SELECT Students.StudentID, Students.Name, Students.Birthday, Students.MothersName, Classes.ClassName, Cities.Name, Cities.Name,
PostalCode.PostalCode, Street.StreetName, Students.Number
FROM Students
INNER JOIN Classes ON Students.ClassID = Classes.ClassID
INNER JOIN Cities ON Students.BirthPlaceID = Cities.CityID
INNER JOIN Cities ON Students.CityID = Cities.CityID
INNER JOIN PostalCode ON Students.PostalCodeID = PostalCode.PostalCodeID
INNER JOIN Utca ON Students.StreetID = Streets.StreetID
i've tried this way but it only results in error.
You create an alias for the table in your select and then you can access the same table with 2 different identifies. I did the same (created alias) for the values in your select statement to identify which city is which.
SELECT Students.StudentID, Students.Name, Students.Birthday, Students.MothersName, Classes.ClassName, StudentBirthCities.Name as StudentBirthCityName, StudentCity.Name as StudentCityName,
PostalCode.PostalCode, Street.StreetName, Students.Number
FROM Students
INNER JOIN Classes ON Students.ClassID = Classes.ClassID
INNER JOIN Cities StudentBirthCities ON Students.BirthPlaceID = StudentBirthCities.CityID
INNER JOIN Cities StudentCity ON Students.CityID = StudentCity.CityID
INNER JOIN PostalCode ON Students.PostalCodeID = PostalCode.PostalCodeID
INNER JOIN Utca ON Students.StreetID = Streets.StreetID

Missing "empty" records

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

LINQ join queries on multiple table

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().

Convert a n-m relationship join query to Linq

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});

Retrieving Data From Three Table

cmd =new SqlCommand("Select i.InvoiceNo,i.TotalAmount,
i.PaymentStatus,m.MovieID,m.MovieName, s.CompanyName
From InvoiceDetails i INNER JOIN Movie m ON
i.InvoiceNo=m.InvoiceNo and Supplier s
Inner Join Movie m ON s.SupplierID=m.SupplierID
Where SupplierID=#supplierID AND
CompanyName=#companyName AND PaymentStatus=#ddlPaymentStatus",
conPayment);
This is the wrong Select Statement and How can I modify it in order successful to get the data from three table.
The one thing that jumps out at me is one JOIN done incorrectly:
You have:
...
from InvoiceDetails i
inner join Movie m on i.InvoiceNo = m.InvoiceNo
and Supplier s
inner join Movie m on s.SupplierID = m.SupplierID
...
It should be:
select i.InvoiceNo,i.TotalAmount,i.PaymentStatus,m.MovieID,m.MovieName,s.CompanyName
from InvoiceDetails i
inner join Movie m on i.InvoiceNo = m.InvoiceNo
inner join Supplier s on s.SupplierID = m.SupplierID
where s.SupplierID = #supplierID
and s.CompanyName = #companyName
and i.PaymentStatus = #ddlPaymentStatus
You already joined Movie once, you have to then JOIN on supplier also. The way you are doing it will surely give you a syntax error. Once for having two tables with the same alias, and then it would also complaint about not knowing supplier s, since it would be expecting a column from the tables joined before that.

Categories

Resources