Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq Select All Items Matching Array

Tags:

c#

linq

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;}
}

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.

like image 390
Gavin Coates Avatar asked Aug 22 '13 15:08

Gavin Coates


1 Answers

You can filter your list with LINQ Where using Contains method:

var result = list.Where(x => employees.Contains(x.EmployeeID));
like image 150
cuongle Avatar answered Oct 20 '22 17:10

cuongle