1
votes

I have some classes:

public Department {
  public HashSet<Employee> Managers {get; set;}
  public HashSet<Employee> Workers {get; set;}
  public HashSet<Asset> Assets {get; set;}
}

I am using IQueryable to get a deparment collection:

IQueryable<Department> deparments = Context.Department.Where (
dept => dept.CompanyId = id)
.include (dept.Managers.Where (emp => emp.level == Position.Manager) as Managers)
.include (dept.Workers.Where (emp => emp.level == Position.Worker) as Workers)
.include (dept.Asset)

It gave errors when the query is executed. What is the right way to do this? Here is the error msg:

"The Include property lambda expression 'dept => {from Employee emp in dept.Employee where ([emp].Position == Position.Manager) select [emp]}' is invalid. The expression should represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, specify an explicitly typed lambda parameter of the target type, E.g. '(Derived d) => d.MyProperty'.

For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393."

1
Can you provide the error perhaps?Schwarzie2478
The error is needed so we have an indicator as to what execution is happening.Kieran Devlin
Updated error msg.user3097695

1 Answers

1
votes

As your Managers and Workers properties are the direct children of Department class and you don't make reference of Department in Employee class so you can't use Include() for this purpose.

I think you should do this by using subqueries.

var query = (from d in Contex.Department
             where d.CompanyId == id
             select new Department{
                 Managers = d.Managers.where(m => m.level == Position.Manager),
                 Workers = d.Workers.where(w => w.level == Position.Worker),
                 Asset = d.Assets,
             });

Hopefully, it will resolve your problem.