I'd like to get with LINQ an employee list, these employees must have in the TypeOfWorks
list the typeofWork
passed (Id
) in argument
public class Employee
{
public virtual IList<EmployeeTypeOfWork> TypeOfWorks { get; set; }
}
public class EmployeeTypeOfWork
{
public virtual Guid Id { get; set; }
public virtual Employee Employee { get; set; }
public virtual TypeOfWork TypeOfWork { get; set; }
}
public class TypeOfWork
{
public virtual Guid Id { get; set; }
}
public IList<Employee> ListWithTypeOfWork(IList<Employee> Employees,
Guid typeOfWorkId)
{
?????
}
I tried this but I missed something I think
var res = from p in Employees
where (from pp in p.TypeOfWorks
where pp.TypeOfWork.Id == guid select pp.Id).Contains(p.Id)
select p;
Try the following
var res = Employees
.Where(x => x.TypeOfWorks.Any(w => w.Id == guid))
public IEnumerable<Employee> ListWithTypeOfWork(IList<Employee> Employees, Guid typeOfWorkId)
{
return from emp in Employees
where emp.TypeOfWorks.Any(x => x != null && x.Id == typeOfWorkId)
select emp;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With