2

The current code lists out each Department Name and I can Browse each employee that works in the department. How can I filter by shift? I have been unsuccessful using any type of where statement.

I have two Models:

Department

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

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

and Employee

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

Controller

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);
}
Hugh
  • 35
  • 1
  • 7
  • you want to list employees belongs to a purticular shift ? – Shyju Jan 29 '14 at 21:04
  • Im guessing you are using Entity Framework? If so you could use Relationship Fixup. I asked a question about it a while ago, here is the link: http://stackoverflow.com/questions/19012116/fetch-entity-framework-objects-with-partially-loaded-collections – Andrei Dvoynos Jan 29 '14 at 21:02
  • Yes, I'm using Entity Framework, the Index method above functions to list out each Department Name, the Browse method pools the employees that belong to that department. I would like to Display only the "1st" Shift employees. – Hugh Jan 29 '14 at 21:52
  • It's a pain that EF doesn't support this out of the box. Here's a way to do it: http://stackoverflow.com/a/17014802/861716. It's been a feature request since 2011: https://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/1015345-allow-filtering-for-include-extension-method – Gert Arnold Jan 30 '14 at 21:10

2 Answers2

0

you can add where to your lambda

var department = db.Department.Where(d => d.Name == "Filter Item Here").OrderBy(d => d.Name).ToList();
Matt Bodily
  • 6,403
  • 4
  • 29
  • 48
  • Well, if this is what he wants, I guess i misread the question :P – Andrei Dvoynos Jan 29 '14 at 21:05
  • he said he has been unsuccessful using any kind of where statement so that is why I put this answer – Matt Bodily Jan 29 '14 at 21:06
  • Yeah, I misunderstood and thought he wanted to filter Employees inside Departments, in that case my answer would have helped. If he just wants to filter departments then your answer is the correct one. – Andrei Dvoynos Jan 29 '14 at 21:09
0

The inital intent was to filter this in the controller, I ended up filtering from the View. @foreach (var employee in Model.Employee.Where(s => s.Shift == "1st")) { <li> </li> }

I will continue working at this and update once I complete the task.

Hugh
  • 35
  • 1
  • 7