Linq选择所有项目匹配数组 [英] Linq Select All Items Matching Array
问题描述
我有一个IEnumerable<Objects.LabourHours>
类型的数据收集,其中包含各种雇员的劳动记录.我希望过滤列表并仅返回选定雇员的记录,该记录由包含EmployeeID
的int[] employees
列表指定.
I have a data collection of type IEnumerable<Objects.LabourHours>
containing labour records for various employees. I wish to filter the list and return only records for selected employees, which is specified by a list of int[] employees
containing the EmployeeID
s.
class LabourHours
{
public int ID {get;set;}
public int EmployeeID {get;set;}
public int HoursWorked {get;set;}
}
我将如何处理?我确信这已经被问过了,但是我在这里找不到任何类似的东西.我发现最接近的是按用户ID对记录进行分组,这不是我所需要的-我需要实际的记录.
How would I go about this? I am sure this has been asked before but I can't find anything similar on here. The closest I have found involves grouping the records by UserID, which is not what I need - I need the actual records.
推荐答案
您可以使用Contains
方法通过LINQ Where
过滤列表:
You can filter your list with LINQ Where
using Contains
method:
var result = list.Where(x => employees.Contains(x.EmployeeID));
这篇关于Linq选择所有项目匹配数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!