实体框架Linq JOIN和WHERE

时间:2016-12-28 08:47:47

标签: c# sql entity-framework linq

我正在使用Entity Framework,我的模型有这样的关系

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

但是由于我使用的是Entity Framework,因此这段代码无法正常连接表。我需要获得Employee Model及其相关表及其结果

到目前为止,这显示了没有过滤器的整个数据。我需要的是如何选择性地过滤它

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

2 个答案:

答案 0 :(得分: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();
 }

答案 1 :(得分: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())