I would like to use LINQ's ForEach construct and am not sure how to convert the following.
My current implementation:
var employees = (from e in employeeDepartmentList select e.Employee).ToList();
employeeList = new EmployeeList();
foreach (var emp in employees)
{
employeeList.Add(emp);
}
I am thinking something like this:
employeeList = new EmployeeList();
var employees = (from e in employeeDepartmentList select e.Employee).ToList().ForEach(emp => employeeList.Add(emp));
ForEach
is not a LINQ method, it is a method of List<>
. In this simple scenario, why even use that? employeeList.AddRange(employees)
would be even simpler. Even further, if employees
is already a list, do you need employeeList
?
As for more advice on using foreach vs. ForEach in general, see: http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With