I am new to Linq.
I want to set two values in foreach
statement like this
My actual code is this
foreach (Employee emp in employees
As xanatos said, this is a misuse of ForEach.
If you are going to use linq to handle this, I would do it like this:
var departments = employees.SelectMany(x => x.Departments);
foreach (var item in departments)
{
item.SomeProperty = null;
}
collection.AddRange(departments);
However, the Loop approach is more readable and therefore more maintainable.
You can use Array.ForEach()
Array.ForEach(employees, employee => {
Array.ForEach(employee.Departments, department => department.SomeProperty = null);
Collection.AddRange(employee.Departments);
});