이엔티티 프레임 워크 Linq에 가입하고 내가 엔티티 프레임 워크 내 모델을 사용하고
같은 관계가 어디에서 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;
}
아래의 답변을 확인하고 테스트 해보십시오. –