简体   繁体   中英

Entity Framework Linq JOIN and WHERE

I am using Entity Framework and my models has a relationship like this

在此处输入图片说明

I have a report that has 3 optional filters: Date From, Date To, Employee No

in normal sql i would like this

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

But since I'm using Entity Framework, this code does not properly get joined table. I need to get the Employee Model with its related table and its result

So far got this displaying the whole data with no filters. What I need is how can I filter it optionally

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

you will return data 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();
 }

.... try something like this:

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

The secret in EF is to leave your entity as Queriable (so to don't do ToList())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM