简体   繁体   中英

Linq - Filter a Result set based on another query

In my DB structure Each EmployeeId can have multiple BusinessId's

In my 1st query I am getting the BusinessId's of an employee based his EmployeeId

List<int> businessId = EMPDB.BusinessEmployees.where(a =>  a.EmployeeId == 5678 && a.Active == true).Select(S => s.BusinessId).ToList();

Out of the above BusinessIds I want to filter and get only those BusinessIds who have non compliance Ie there BusinessId is present in the BusinessCompliance table.

List<int> complianceBusinessIds = EMPDB.BusinessCompliances.where( m=> businessId.Contains(m.BusinessId)).ToList(); —- this is throwing error and I am not able to achieve this

Did you try a join?

        from be in EMPDB.BusinessEmployees
        join bc in EMPDB.BusinessCompliances on be.BusinessId equals bc.BusinessId
        where be.EmployeeID == 5678
        select bc;

It should be the better performance way to answer things like this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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