我使用實體框架和我的模型具有這樣的實體框架的LINQ JOIN和WHERE
的關係我有一個有3個可選的過濾器報告:日期從,日期到,員工沒有
在普通的SQL我想這
public List<Employee> SearchAttendance(string fromDate, string toDate, string employeeNo)
{
string query = "SELECT e.first_name, e.last_name, i.employeeNo, t.timeIn, t.timeOut, t.imagePath
FROM Employees AS e LEFT JOIN EmploymentInfo AS i ON e.id = i.employeeId
LEFT JOIN Trackers AS t ON i.id = t.employeeId ";
if (fromDate != "")
{
if (toDate == "")
{
query += "where t.timeIn = '" + Datetime.Parse(fromDate) + "' ";
}
else
{
query += "where t.timeIn >= '" + Datetime.Parse(fromDate) + "' ";
query += "and t.timeIn <= '" + DateTime.Parse(toDate) + "' ";
}
if (employeeNo != "")
{
query += "and ";
}
}
if (employeeNo != "")
{
if (fromDate == "")
{
query += "where ";
}
query += "i.employeeNo = '" + employeeNo + "' ";
}
query += "ORDER BY t.timeIn DESC";
var res = _context.Database.SqlQuery<Employee>(query).ToList();
return res;
}
但自從我使用實體框架,這個代碼不正確地得到連接表。我需要獲得員工模型及其相關表格及其結果
到目前爲止,顯示的是沒有過濾器的整個數據。我需要的是我怎麼可以過濾它可選
public List<Employee> SearchAttendance(string fromDate, string toDate, string employeeNo)
{
var employee = _context.Employees
.Include(i => i.EmploymentInfoes)
.Include(i => i.EmploymentInfoes.Select(c => c.Trackers))
.ToList();
return employee;
}
檢查下面的答案並測試它.. watting你的測試結果?! –