C# Linq Joining tables with multi-level - c#

I am trying to query the 3rd level table ef_staff table 3 times to get 3 diff staff objects for each row. How to translate this in LINQ?
SELECT a.a_appraisalid, a.a_year, c.s_staffName, c2.s_staffName, c3.s_staffName
FROM ef_appraisal a, idp_application b, ef_staff c, ef_staff c2, ef_staff c3
WHERE a.a_appraisalid = b.a_appraisalid AND
a.a_staffid = c.s_staffid AND
a.a_appraisedby = c2.s_staffid AND
a.a_reviewedby = c3.s_staffid
I have been trying many ways but there is still an error 'Type Inference Failed' in the 2nd & 3rd joining of Staff. What am I missing here?
from application in applications
join appraisal in pmsEntities.ef_appraisal on application.a_appraisalid equals appraisal.a_appraisalid
join staff in pmsEntities.ef_staff on appraisal.a_staffid equals staff.s_staffid
join appraiser in pmsEntities.ef_staff on staff.s_appraisedby equals appraiser.s_staffid into ap
from appraiser in ap.DefaultIfEmpty()
join reviewer in pmsEntities.ef_staff on staff.s_reviewedby equals reviewer.s_staffid into rv
from reviewer in rv.DefaultIfEmpty()
join company in pmsEntities.ef_company on appraisal.a_companyid equals company.c_companyid into jc
from company in jc.DefaultIfEmpty()
select appraisal, staff.staffName, appraiser.staffName, reviewer.staffName, company.compName

I fixed the mistake. The first level joining of staff object should be linked to the 2nd level of staff object as below:
join staff in pmsEntities.ef_staff on appraisal.a_staffid equals staff.s_staffid into staffj
from staff1 in staffj
join appraiser in pmsEntities.ef_staff on staff1.s_appraisedby equals appraiser.s_icno into staff2
`
Hope it helps

Related

How To Fix Join Problem Show Same Results

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

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

Multiple JOIN in Linq-to-SQL

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;

Multiple table join with multiple table where clause

I have a query that's something like this.
Select a.*
from table1 a
inner join table2 b on a.field1 = b.field1
inner join table3 c on b.field2 = c.field2
where b.field4 = beta and c.field5 = gamma.
On LINQ, I tried to do that this way:
var query = (from a in table1
join b in table2 on a["field1"] equals b["field1"]
join c in table3 on b["field2"] equals c["field2"]
where (b["field4"] == beta && c["field5"] == gamma)
select a).ToList();
But for some reason, when I try to do this I get an error that says that the entity "table2" doesn't have the field Name = "field5", as though as the where clause was all about the last joined table and the other ones were unaccessible. Furthermore, the compiler doesn't seem to notice neither, because it lets me write c["field5"] == gamma with no warning.
Any ideas? Am I writing this wrong?
Thanks
See these links:
How to: Perform Inner Joins (C# Programming Guide)
What is the syntax for an inner join in linq to sql?
Why you don't create View in database, and Select your data from View in LINQ?

Linq to SQL - Group By and Count

I'm trying to convert this query (already working)
SELECT Building.NAME, COUNT([User].ID)
FROM BuildingUser
INNER JOIN Building ON Building.ID = BuildingUser.ID_BUILDING
INNER JOIN [User] ON [User].ID = BuildingUser.ID_USER
GROUP BY Building.NAME
To Linq to SQL, but I don't know what I'm doing wrong. Look at my trying
from buildinguser in db.GetTable<BuildingUser>()
join building in db.GetTable<Building>()
on buildinguser.ID_BUILDING equals building.ID
join user in db.GetTable<User>()
on buildinguser.ID_USER equals user.ID
group building by building.NAME into grpBuilding
select new
{
building = grpBuilding.Key,
users =
};
I just need to group my Buildings and count how many users each one has.
Simply use the the Count method:
from buildinguser in db.GetTable<BuildingUser>()
join building in db.GetTable<Building>()
on buildinguser.ID_BUILDING equals building.ID
join user in db.GetTable<User>()
on buildinguser.ID_USER equals user.ID
group building by building.NAME into grpBuilding
select new
{
building = grpBuilding.Key,
users = grpBuilding.Count()
};

Categories

Resources