假设我有这样的业务对象,
class Employee { public string name; public int id; public string desgination; public int grade; } List<Employee> lstEmp = new List<Employee>() { new Employee() { name="A",desgination="SE",id=1},new Employee() { name="b",desgination="TL",id=2},new Employee() { name="c",desgination="PL",id=3},new Employee() { name="d",id=4},new Employee() { name="e",desgination="SSE",id=5},};
如果我想把员工年级更新为3,其名称为“SE”,那么我必须写这样的东西
lstEmp=lstEmp.Select(x => { x.grade = (x.desgination == "SE") ? 3 : x.grade; return x; }).ToList();
但是在这里使用select时,它会每次都会生成新的employee对象,而不是更新现有的lstEmp,所以我必须将更新的列表重新发送到lstEmp.