我有一组Employee对象
IEnumerable<Employee> employees;
员工看起来像这样:
class Employee
{
int Id {get; set;}
int Name {get; set;}
}
我有第二个系列:
IEnumerable<int> fired;
我希望根据Employee的Id属性进行员工交叉并解雇。类似的东西:
employees.Intersect(fired)
这当然不会起作用,因为员工和解雇是不同的类型。我知道我可以这样做:
List<int> empIds = null;
employees.Foreach(x => empIds.Add(x.Id);
然后执行empIds并触发的交集,然后将其合并。但必须有一种方法可以在不声明新列表的情况下执行此操作。我的目标是建立一种方法,通过引用传递员工集合,它将过滤掉&#39;被解雇的员工:
void YourRFired(ref IEnumerable<employee> employees)
{
IEnumerable<int> fired = .....
// intersect employee and fired here to get a list of employees that are
not fired.
}
由于
答案 0 :(得分:1)
在哪里?
var notFiredEmployees = employees.Where(e=>!fired.Contains(e.Id));
答案 1 :(得分:0)
类似
IEnumerable<Employee> employees;
IEnumerable<int> firedEmployeeIds;
//return all employees who are not fired
return employees
.GroupJoin(firedEmployeeIds, e => e.Id, f => f, (employee, ids) => new
{
employee,
fired = ids.Any() == false
})
.Where(x => x.fired == false)
.Select(x => x.employee);