来自多个项目的条件LINQ匹配 [英] conditional LINQ match from multiple items

查看:99
本文介绍了来自多个项目的条件LINQ匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我是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.

推荐答案

这是一个相当简单的示例,它可以执行以下操作说你需要

This is a fairly simple example that does what say that you need

// 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匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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