问题
How can I create an efficient Linq-to-Entities query when I have an array of parameters in a WHERE clause with OR condition? The array length can be anything.
For example: from employees -> return all employees that have EmployeeID of 1, 2 or 3.
Stupid way of doing this would be:
For index = 0 To employeeArray.Lenght-1
FindID = employeeArray(index)
Dim query = From emp In _context.Employees
Where emp.EmployeeID = FindID
Select emp
Next
How can I achieve this effectively?
回答1:
Dim query = From emp In _context.Employees
Where employeeArray.Contains(emp.EmployeeID)
Select emp
Use Contains
method instead - it will be transformed into IN
clause within SQL query generated by Entity Framework.
来源:https://stackoverflow.com/questions/15966395/linq-to-entities-query-with-array-of-where-conditions