list中某个元素的值

list中某个元素的值

1、改变list中某个元素的值

    public class tb_SensorRecordModel
{
public int ID { get; set; }
public decimal Value1 { get; set; }
}
            List<tb_SensorRecordModel> list = new List<tb_SensorRecordModel>();
list.Add(new tb_SensorRecordModel { ID = , Value1 = });
list.Add(new tb_SensorRecordModel { ID = , Value1 = });
list.Add(new tb_SensorRecordModel { ID = , Value1 = }); //改变list中某个元素值
var model = list.Where(c => c.ID == ).FirstOrDefault();
model.Value1 = ; list.ForEach(c =>
{
//打印的数据表明 list中的那个元素 确实被改变了
//知识:引用、地址
Console.WriteLine($"{c.ID},{c.Value1}");
});
Console.Read();
return;

2、替换某一段数据

            List<tb_SensorRecordModel> list1 = new List<tb_SensorRecordModel>();
list1.Add(new tb_SensorRecordModel { ID = , Value1 = });
list1.Add(new tb_SensorRecordModel { ID = , Value1 = });
list1.Add(new tb_SensorRecordModel { ID = , Value1 = });
list1.Add(new tb_SensorRecordModel { ID = , Value1 = });
list1.Add(new tb_SensorRecordModel { ID = , Value1 = }); //构造新的一段数据
List<tb_SensorRecordModel> list2 = new List<tb_SensorRecordModel>();
list2.Add(new tb_SensorRecordModel { ID = , Value1 = });
list2.Add(new tb_SensorRecordModel { ID = , Value1 = }); //删除 旧的 那段数据
list1.RemoveRange(, ); //将新的 这段数据 插入到 指定位置
list1.InsertRange(, list2); list1.ForEach(c =>
{
Console.WriteLine($"{c.ID},{c.Value1}");
});
Console.Read();
return;
05-11 17:08