1

I have a department object, and that object contains a list of employees. How do I get a department that a specific employee works in? I have a generic FindAll

IQueryable<Department> FindAll(params Expression<Func<Department, object>>[] includeProperties)

I then tried

FindAll().Where(x => x.Employee.Any(y => y.Name == name)).FirstOrDefault();
3
  • 2
    Im guessing .Name is a String. Should it not be y.Name.Equals(name) Commented Dec 8, 2015 at 9:51
  • Aren't there any built in function in entity framework like Find()? Commented Dec 8, 2015 at 9:56
  • Please show your deparment model Commented Dec 8, 2015 at 10:37

1 Answer 1

2

Do you want to load a related entity?

You could do this:

    var employee = context.Employee.FirstOrDefault(y=>y.Name==name);
    if(employee!=null)
    {
      context.Entry(employee).Reference("Department").Load();
      var deparment = employee.Department;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Shouldn't that be context.Entry(employee) (lower-case e on employee)? And shouldn't it be var department = employee.Department?

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.