EF Model Image References
I was planned to read data from database and then using INNER JOIN in C# WebApi controller as the picture shown below.
Below query is for Inner Join references:
Select FirstName, LastName, Gender, Salary, E.Department_id, Department_Name
from Employee E
INNER JOIN Department D on D.department_id = E.department_id
UPDATE
The answer had been confirmed by the following code Solution for joining data via DTO method
public class JoinController: ApiController
{
DepartmentServicesEntities DSE = new DepartmentServicesEntities();
[Route("Api")]
[HttpGet]
public object JoinStatement()
{
using (DSE)
{
var result = (from e in DSE.employee join d
in DSE.department on e.department_id equals d.department_id
select new {
FirstName = e.FirstName,
LastName = e.LastName,
Gender = e.Gender,
Salary = Salary,
Department_id = e.department_id,
Department_Name = d.department_name
}).ToList();
// TODO utilize the above result
return result;
}
}
}
}
As for joining multiple table, the solution was here:
namespace WebApiJoinData.Controllers
{
[RoutePrefix("Api")]
public class JoinController : ApiController
{
DepartmentServicesEntities DSE = new DepartmentServicesEntities();
[Route("Api")]
[HttpGet]
public object JoinStatement()
{
using (DSE)
{
var result = (from e in DSE.employees
join d in DSE.departments on e.department_id equals d.department_id
join ws in DSE.workingshifts on e.shift_id equals ws.shift_id
select new
{
FirstName = e.FirstName,
LastName = e.LastName,
Gender = e.Gender,
Salary = e.Salary,
Department_id = e.department_id,
Department_Name = d.department_name,
Shift_id = ws.shift_id,
Duration = ws.duration,
}).ToList();
// TODO utilize the above result
string json = Newtonsoft.Json.JsonConvert.SerializeObject(result, Newtonsoft.Json.Formatting.Indented);
return result;
}
}
}
}
The output following result was shown here: