问问题
124 次
5 回答
1
关于什么:
Employees.Where(e => Customers.Any(c=> c.CustomerName == e.EmployeeName)).ToList()
.ForEach(e=> {
Employees.Remove(e);
Customers.RemoveAll(c => c.CustomerName == e.EmployeeName);
});
于 2013-05-15T02:24:58.620 回答
0
我们试试看:
List<Customer> c = new List<Customer>();
c.Add(new Customer() {CustomerName = "test", Email = "email" });
c.Add(new Customer() { CustomerName = "test1", Email = "email1" });
c.Add(new Customer() { CustomerName = "test2", Email = "email2" });
c.Add(new Customer() { CustomerName = "test3", Email = "email3" });
List<Employee> e = new List<Employee>();
e.Add(new Employee() { EmployeeName = "test2", EmployeeID = "1" });
e.Add(new Employee() { EmployeeName = "test1", EmployeeID = "2" });
e.Add(new Employee() { EmployeeName = "test5", EmployeeID = "3" });
e.Add(new Employee() { EmployeeName = "test4", EmployeeID = "4" });
//remove from the Customers list the corresponding Employee
e.RemoveAll(x => c.Any(y => y.CustomerName == x.EmployeeName));
foreach (var employee in e)
{
//TODO:
}
于 2013-05-14T23:35:13.047 回答
0
var inBoth = customers.Join(employees, customer => customer.CustomerName, employee => employee.EmployeeName, (customer, employee) => employee.EmployeeName).ToList();
customers.RemoveAll(customer => inBoth.Contains(customer.CustomerName));
employees.RemoveAll(employee => inBoth.Contains(employee.EmployeeName));
于 2013-05-14T23:54:39.463 回答
0
可以这样做:
var toBeDeleted = employees.Select(e => e.EmployeeName)
.Where(name => customers.Any(c => c.CustomerName == name))
.ToList();
customers.RemoveAll(c => toBeDeleted.Contains(c.CustomerName));
employees.RemoveAll(e => toBeDeleted.Contains(e.EmployeeName));
您也许可以改为使用Intersect
来获取应删除的元素。我不知道有任何一行命令可以做到这一点。
于 2013-05-14T23:54:57.997 回答
0
首先创建一个要删除的所有客户的列表和要删除的所有员工的另一个列表。然后从您的原始列表中删除所有这些。我对 lambda 不够坚定,无法从内存中写出来,但它应该是可行的。
于 2013-05-14T23:21:02.640 回答