0

This might be a very easy thing to do but i couldnt find a way.

I am trying to return a view where DepartmentID is 1 and DirectorID is 2. DepartmentID is in Employee class, and DirectorID is in Director class.

 public class Employee
{
    public int EmployeeID { get; set; }
    public string Name { get; set; }
 }
  public class Director
{
    public int DirectorID { get; set; }
    public string Name { get; set; }
 }

And this is my ActionResult but this is not meeting my requirements. I need to include DirectorID too in my where statement. Should i use &&?

   public ActionResult Index()
    {
     return View(db.Employee.Where(x => x.DepartmentID == 1)); 
    }
3
  • 1
    Can you post code that demonstrates the relationship between your Employee entity and your Director entity, also something that shows how DepartmentId relates to employee because the code you have posted does not show a DepartmentId property on your Employee class. Commented Sep 18, 2014 at 16:09
  • 2
    You are selecting from the Employee collection. Is the employee collection aware of the director ID, or would you need some sort of calculated value that joins Employee with director? Commented Sep 18, 2014 at 16:10
  • Actually, you both are right. I need to have a relationship between these two tables. Thanks for the feedback. Commented Sep 18, 2014 at 16:26

1 Answer 1

1

There's a number of ways to handle this, but which is appropriate depends on the relationship, or lack there of, between these entities, which you haven't disclosed.

First, let's take the basic example of having an actual relationship. Assuming you had something like:

public class Employee
{
    public int EmployeeID { get; set; }
    public string Name { get; set; }

    public int DirectorId { get; set; }
    public virtual Director Director { get; set; }
}

In other words, there's a one-to-many between Employee and Director where an employee has one director and a director has many employees, then in your action you can do:

db.Employees.Where(x => x.DepartmentID == 1 && x.DirectorId == 2).Include("Director");

Which will net you a list of employees, each with a Director instance accessible through the Director property on Employee. You could then work with either the Employee instance or the Director instances off of each Employee instance at will in your view.

If there's not a direct relationship, then you need to query each separately and combine them in a view model. For example:

public class EmployeesDirectorViewModel
{
    public List<Employee> Employees { get; set; }

    public Director Director { get; set; }
}

Then in your action:

var employees = db.Employee.Where(x => x.DepartmentID == 1);
var director = db.Directors.Where(x => x.DirectorID == 2);

var model = new EmployeesDirectorViewModel
{
    Employees = employees,
    Director = director
}

return View(model);

However, it should be noted that since we're assuming there's no direct relationship in this example, there's no way to filter the employees by which director they belong to.

A third option is a combo of the previous to. Even if you have a direct relationship between an employee and a director, you can still use a view model to better organize your data. The goal of a view model is to optimize the data for what the view needs, so you can include subsets of properties, additional properties not on the entities, calculations and aggregations, etc.

Finally, it's possible there might be a relationship, just not a direct one. For example, you might have something like:

public class Employee
{
    ...

    public int DepartmentID { get; set; }
    public virtual Department Department { get; set; }
}

public class Department
{
    ...

    public int DirectorID { get; set; }
    public Director Director { get; set; }
}

You can handle this in a very similar way to my first example, you just need to traverse the relationship in your query:

db.Employees.Where(x => x.DepartmentID == 1 && x.Department.DirectorId == 2)
    .Include("Department.Director");

Now, that example is contrived because if you're selecting the department by id, there's no point in further conditioning it with a particular director as well. The director is either the director or it's not.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.