从列表中删除对象单个属性

时间:2017-11-05 20:10:08

标签: c# linq

我想从列表中删除对象单个属性。我上课了,

Public class Employee
{
 public int Id {get;set;} 
 public string Name {get;set;}
 public string Address {get;set;}
}

我有清单,

List<Employee> lstEmployee = new List<Employee>();

认为它有价值,

lstEmployee[0] = { Id = 0 , Name = "A" , Address = "ABC" }
lstEmployee[1] = { Id = 0 , Name = "B" , Address = "XYZ" }
lstEmployee[2] = { Id = 0 , Name = "C" , Address = "PQR" }

我想删除属性 ID ,其余记录将保持不变。

lstEmployee[0] = { Name = "A" , Address = "ABC" }
lstEmployee[1] = { Name = "B" , Address = "XYZ" }
lstEmployee[2] = { Name = "C" , Address = "PQR" }

有可能实现这一目标吗? (任何其他或替代方式)

1 个答案:

答案 0 :(得分:0)

这是不可能的,因为一旦设置了类定义,那就是它。

您可以做的最好的方法是检索仅NameAddress的数组  对象中包含的属性是使用LINQ对每个元素执行操作,在这种情况下,您可以检索一个匿名类型数组,准确指定所需的属性。

示例:

var result = (from e in lstEmployee
              select new { e.Name, e.Address }).ToArray();