2016-12-28 76 views
0

我使用實體框架和我的模型具有這樣的實體框架的LINQ JOIN和WHERE

enter image description here

的關係我有一個有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; 
} 
+0

檢查下面的答案並測試它.. watting你的測試結果?! –

回答

1

....嘗試這樣的事:

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)); 

     if (!string.IsNullOrEmpty(fromDate)) 
     { 
      employee = employee.Where(xx => xx.timeIn <= DateTime.Parse(fromDate)); 
     } 

     if (!string.IsNullOrEmpty(toDate)) 
     { 
      employee = employee.Where(xx => xx.timeIn >= DateTime.Parse(toDate)); 
     } 

     if (!string.IsNullOrEmpty(employeeNo)) 
     { 
      employee = employee.Where(xx => xx.employeeNo == employeeNo); 
     } 

     return employee.ToList(); 
    } 

的祕密EF是離開你的實體作爲可查詢(所以不要做ToList())

+0

問題是當我使用xx => xx時。我只能訪問Employee模型的屬性 – natsu1627

+0

是的......但是如果你還需要訪問像你的Trackers這樣的子實體if(!string.IsNullOrEmpty(fromDate))employee = employee.Where( xx => xx.EmploymentInfoes.Select(c => c.Trackers).Any(tt => tt。timeIn <= DateTime.Parse(fromDate)) –

1

將返回Quarable數據F

public List<Employees> SearchAttendance(string fromDate, string toDate, string employeeNo) 
{ 
     var employees = _context.Employees 
         .Include(i => i.EmploymentInfoes) 
         .Include(i => i.EmploymentInfoes.Select(c => c.Trackers)) 
         .Select(i=> new { // your properties you need }) 
         .AsQueryable(); 

     if (fromDate != "") 
     { 
      employees = employees.where(t => t.timeIn >= DateTime.Parse(fromDate)); 
     } 

     if (toDate != "") 
     { 
      employees = employees.where(t => t.timeIn <= DateTime.Parse(toDate)); 
     } 

     if (employeeNo != "") 
     { 
      employees = employees.where(t => t.employeeNo == employeeNo); 
     } 


     return employees.ToList(); 
} 
+0

問題是當我使用t => t時。我只能訪問Employee模型的屬性 – natsu1627

+0

選擇你需要的屬性'.Select(i => new {//你需要的屬性}' –

+0

@ natsu1627這是否解決了你的問題? –