2016-12-28 1 views
0

엔티티 프레임 워크 Linq에 가입하고 내가 엔티티 프레임 워크 내 모델을 사용하고

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

     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에서 : 내가 필요한 것은 내가 .... 선택적으로

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

아래의 답변을 확인하고 테스트 해보십시오. –

답변

1

f를 Quarable 데이터를 반환합니다 그것을 필터링 같은 것을 시도 할 수있는 방법입니다 엔티티를 Queriable로 남겨 두는 것입니다 (ToList()를 수행하지 않으려면)

+0

문제는 내가 xx => xx를 사용할 때입니다. 직원 모델 – natsu1627

+0

의 속성에만 액세스 할 수 있지만 트래커와 같은 하위 엔티티에 액세스해야하는 경우 if (! string.IsNullOrEmpty (fromDate)) employee = employee.Where xx => xx.EmploymentInfoes.Select (c => c.Trackers) .Any (tt => tt. timeIn <= DateTime.Parse (fromDate)) –

1

당신이

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를 사용할 때입니다. 직원 모델 – natsu1627

+0

의 속성에만 액세스 할 수 있습니다. 필요한 금액을 선택하십시오. 선택 (i => new {// 필요한 재산})' –

+0

@ natsu1627 문제가 해결 되었습니까? –

관련 문제