如何从“包含”表中过滤?

时间:2014-01-29 20:57:57

标签: c# linq asp.net-mvc-4

当前代码列出了每个部门名称,我可以浏览在该部门工作的每个员工。如何按班次过滤?我使用任何类型的where语句都没有成功。

我有两个型号:

public class Department
{
    public int DepartmentID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public List<Employee> Employee { get; set; }
}

员工

public class Employee
{
    public int EmployeeID { get; set; }
    public string LastName { get; set; }
    [Display(Name = "First Name Middle Initial")]
    public string FirstMidName { get; set; }
    public DateTime HireDate { get; set; }
    public string Shift { get; set; }
    public int DepartmentID { get; set; }
    public string FullName
    {
        get
        {
            return LastName + ", " + FirstMidName;
        }
    }
    public virtual Department Department { get; set; }
}

控制器

public ActionResult Index()
{
    var department = db.Department.OrderBy(d => d.Name).ToList();
    return View(department);
}

public ActionResult Browse(string department)
{
    // Retrieve Department and its Associated Employees from database
    var deptModel = db.Department.Include("Employee")
   .Single(g => g.Name == department);

    return View(deptModel);
}

2 个答案:

答案 0 :(得分:0)

你可以添加lambda的位置

var department = db.Department.Where(d => d.Name == "Filter Item Here").OrderBy(d => d.Name).ToList();

答案 1 :(得分:0)

最初的意图是在控制器中对此进行过滤,最后我从View中过滤。 @foreach (var employee in Model.Employee.Where(s => s.Shift == "1st")) { <li> </li> }

我将继续努力,并在完成任务后更新。