Linq选择所有项目匹配数组 [英] Linq Select All Items Matching Array

查看:39
本文介绍了Linq选择所有项目匹配数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个IEnumerable<Objects.LabourHours>类型的数据收集,其中包含各种雇员的劳动记录.我希望过滤列表并仅返回选定雇员的记录,该记录由包含EmployeeIDint[] 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 EmployeeIDs.

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屋!

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