簡體   English   中英

實體框架Linq JOIN和WHERE

[英]Entity Framework Linq JOIN and WHERE

我正在使用實體框架,並且我的模型具有這樣的關系

在此處輸入圖片說明

我有一個包含3個可選過濾器的報告:Date From,Date To,Employee No

在普通的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,因此此代碼無法正確連接表。 我需要獲取員工模型及其相關表和結果

到目前為止,它無需篩選即可顯示整個數據。 我需要的是如何可選地對其進行過濾

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

您將返回數據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();
 }

....嘗試這樣的事情:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM