Linq to Entities查询带有WHERE条件数组 [英] Linq to Entities query with array of WHERE conditions

查看:244
本文介绍了Linq to Entities查询带有WHERE条件数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在具有OR条件的WHERE子句中有参数数组时,如何创建有效的Linq-to-Entities查询?数组长度可以是任何长度.

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.

例如:从雇员->返回所有EmployeeID为1、2或3的雇员.

For example: from employees -> return all employees that have EmployeeID of 1, 2 or 3.

愚蠢的做法是:

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?

推荐答案

Dim query = From emp In _context.Employees
            Where employeeArray.Contains(emp.EmployeeID)
            Select emp

改为使用Contains方法-在Entity Framework生成的SQL查询中,它将转换为IN子句.

Use Contains method instead - it will be transformed into IN clause within SQL query generated by Entity Framework.

这篇关于Linq to Entities查询带有WHERE条件数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆