简体   繁体   English

使用LINQ存在查询

[英]Exists query with LINQ

I'd like to get with LINQ an employee list, these employees must have in the TypeOfWorks list the typeofWork passed ( Id ) in argument 我想让LINQ获得员工名单,这些员工必须在TypeOfWorks列表中获得在参数中传递的typeofWorkId

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM