Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Friends,

I am having a requirement like below.

class Final
{
public string EmployeName{get;set;}
public string Gender{get;set;}
public string DepartId{get;set;}
public string DepartName{get;set;}
public string SalaryId{get;set;}
public string Salary{get;set;}
}

class Emp
{
public string EmployeName{get;set;}
public string Gender{get;set;}

}

class Department
{
public string DepartId{get;set;}
public string DepartName{get;set;}
}
class Salary
{
public string SalaryId{get;set;}
public string Salary{get;set;}
}
Having the list of Final, Emp, Department and Salary like below

lstFinal, lstEmp,lstDepartment,lstSalary


Want a linq query with  final list to be a combination of lstEmp, lstDepartment,lstSalary

Like below table.
Please kindly help me out.

------------------|------|--------|----------|--------|------|-
|-EmployeName-----|Gender|DepartId|DepartName|SalaryId|Salary|
|                                                            |
|                                                            |

What I have tried:

var result= from e Emp
  join d in dept on e.EmployeName equals e.EmployeName
 join g in gend on g.Gender equals g.Gender
select new Final(){EmployeName=e.EmployeName, Gender=e.Gender, DepartId=d.DepartId, DepartName=d.DepartName,SalaryId=s.SalaryId,Salary=s.Salary}
Posted
Updated 24-Apr-18 10:30am
Comments
BillWoodruff 13-Apr-18 14:33pm    
Your Class design does not make sense: the 'Final Class contains duplicate fields of all the other Classes. Please describe what you are modeling, and what your goal is.
santoshkumar413 13-Apr-18 14:55pm    
Actually Emp, Department, Salary will come from three different services i need to combined them into another project in a single class and display into the UI. If duplicate columns are there i vl handle ar UI. but i need the data of three services to combined into single class list.
BillWoodruff 13-Apr-18 15:20pm    
imho: unless any instance of 'emp contains a department and salary 'id ... you have no way of knowing what the data is for that 'emp ... see my point ?

1 solution

If I understand this correctly and your getting the info from a Database, You will be MUCH better off to do this inside a SQL Stored Procedure. SQL will me much faster, easier to work with and less of a Kluge. make your class your final object structure and populate it from a sp call.

And remember even with SQL depending on how many joins / inner joins and records in each table you may be better off to build a temp table using the emp data and empty placeholders for the other data and then update the other fields from their associated tables
 
Share this answer
 

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