Click here to Skip to main content
15,910,878 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
there are two table in the sql,EmployeeCategory And EmployeeStatus, i want a join query that 1st check from employee category table employee category then from employeeStatus table the employee status, in employeestatus table there are multiple records against same employee ,i want to get last one.


C#
EmployeeStatus Table
id    Emp_Id    Status_Id  Shift_Id   sector_Id
1      22         1           1            1
2      22         3           0            1
3      22         1           2            1  
4      22         1           1            1
EmployeeCategory Table
Id     Emp_Id     EmployeeCategory       sector_Id
1       22            A                      1

i am using following query but it does not return any employee
C#
var gtemp = (from es in entities.EmployeeStatus
                                           join ec in entities.EmployeeCategories on es.Employee_Id equals ec.Employee_Id
                                           where es.Status_Id == 7 && es.EmployeeShift_Id == 1 select new
                               {
                                 Sector = es.sector
                           });
Posted
Updated 17-Mar-15 20:01pm
v3
Comments
[no name] 18-Mar-15 1:49am    
It would be helpful if you gave an example of your tables contents and your expected query result.
Maciej Los 18-Mar-15 2:49am    
What you mean by "i want to get last one"? Do you want to get data from EmployeeStatus table with the highest id?
Sajid227 18-Mar-15 2:56am    
yes u right i want to get data from highest id of same employee,as there are multiple records of same employee

1 solution

If i understand you well...

...this should do the work:
C#
var qry = from ec in entities.EmployeeCategories.Where(x=>x.EmployeeCategory=="A")
			join es in entities.EmployeeStatus on ec.Emp_Id equals es.Emp_Id 
			group es by new{Cat=ec.EmployeeCategory, EmpId= es.Emp_Id} into grp
			select new
			{
				Cat = grp.Key.Cat,
				EmpId = grp.Key.EmpId,
				Es_Id = grp.Max(x=>x.id)
			};


Result:
Cat EmpId Es_Id
A   22    4
 
Share this answer
 
Comments
Sajid227 18-Mar-15 6:34am    
how to get list of all employee that full fill that requirement ?
Maciej Los 18-Mar-15 6:45am    
What requirement?
Sajid227 18-Mar-15 7:01am    
var qry1 = from ec in entities.EmployeeCategories.Where(x => x.Category_Id == 1 && x.Sector_Id == sectorId)
join es in entities.EmployeeStatus.Where(x => x.EmployeeShift_Id == 1 || x.EmployeeShift_Id == 2 && x.DutyType_Id == 3 && x.EmployeeStatusType_Id == 1) on ec.Employee_Id equals es.Employee_Id
group es by new { Cat = ec.EmployeeCategoryType_Id, EmpId = es.Employee_Id } into grp
select new
{
Cat = grp.Key.Cat,
EmpId = grp.Key.EmpId,
Es_Id = grp.Max(x => x.Id)
};
i am using this query its working good ,i want some modification, that how to get all employee latest records and check are they full filling this condition,here is my code that is full filling my needs,
.OrderByDescending(x => x.EndDate).GroupBy(x => x.Employee_Id).Select(x => x.LastOrDefault()).Where(x => x.DutyType_Id == 3 && x.EmployeeStatusType_Id == 1).ToList(); in employeeStatus table
Maciej Los 18-Mar-15 7:25am    
Dear Sajid, i'm not sure what you want to achieve, but... The person who have change above query to your needs is you. I do not see your entity model and data. I can only suggest to add another field to group statement to group data by EndDate. Then inside select new statement, fetch data from this group.
King Fisher 18-Mar-15 6:34am    
5+.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900