[英]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 我有一个包含3个可选过滤器的报告:Date From,Date To,Employee No
in normal sql i would like this 在普通的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;
}
But since I'm using Entity Framework, this code does not properly get joined table. 但是,由于我使用的是Entity Framework,因此此代码无法正确连接表。 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 您将返回数据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()) EF的秘诀是让您的实体保持可查询状态(因此不要执行ToList())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.