问题描述
我是linq的新手,并不了解很多,需要您的帮助.我有以下情况:
员工表包含
EmpId作为主键
deptId作为外键
姓名
地址
我有一个匿名对象var deptlist
,其中包含deptId的列表.现在,我想从雇员表中选择具有与我的对象deptlist中相同的任何deptId的所有雇员.
i am new for linq and don''t know much and need your help. i have following scenario:
employee table contains
EmpId as primary key
deptId as foreign key
Name
address
i have an anonymous object var deptlist
in which i contain list of deptId. Now i want to select all employee from employee table that has any of the deptId as i have in my object deptlist.
推荐答案
// Create a 'dummy' deptList as I don't know what yours contains
int[] ids = {1, 4, 5, 9};
var deptList = ids.Select(n => n);
var result = from e in Employees
where (deptList.Contains(e.deptid)
// I am only selecting Lastname, obviously you should select whichever fields you want.
select (e.LastName);
我已经使用e.EmployeeID
而不是e.DeptID
在Northwind数据库示例中对此进行了测试,但这对结果没有影响.
I have tested this against the Northwind database sample using e.EmployeeID
instead of e.DeptID
but that should make no difference to the outcome.
这篇关于来自多个项目的条件LINQ匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!