使用LINQ存在查询

时间:2009-08-11 17:28:18

标签: c# .net linq

我想让LINQ成为员工名单,这些员工必须在TypeOfWorks列表中typeofWork在论证中传递Id

  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)
    {     
        ?????       
    }

我尝试了这个,但我错过了一些我认为

的东西
        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;

2 个答案:

答案 0 :(得分:6)

尝试以下

var res = Employees
  .Where(x => x.TypeOfWorks.Any(w => w.Id == guid))

答案 1 :(得分:3)

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