Linq to Entities query with array of WHERE conditions

旧城冷巷雨未停 提交于 2019-12-25 03:35:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!