简体   繁体   中英

LINQ query for retrieving items that contain any value from a list?

Let's say I have a table containing reports, and each report has an Employee ID for the employee assigned to it.

Let's also say I have a list of employee ID, like:

[ "1001", "1002", "1003", "1004", "1005", "1006", "1007"]

What LINQ query will return all the reports that have those employee IDs? If it were just a single criteria (like if I wanted everything older than a year), I could just do

query.Where(report => report.LaunchedDate < lastYear)

But how do I do it for when I have many items? Do I need to use a loop or is there a LINQ method for enumerating the list of employee IDs?

Sounds like you are looking for something along the lines of:

var employeeIds = ["1001", "1002", "1003", "1004", "1005", "1006", "1007"];

query.Where(report => employeeIds.Contains(report.EmployeeId));

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