• C# 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 = 1, Value1 = 1 });
                list.Add(new tb_SensorRecordModel { ID = 2, Value1 = 2 });
                list.Add(new tb_SensorRecordModel { ID = 3, Value1 = 3 });
    
                //改变list中某个元素值
                var model = list.Where(c => c.ID == 2).FirstOrDefault();
                model.Value1 = 2222;
    
                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 = 1, Value1 = 1 });
                list1.Add(new tb_SensorRecordModel { ID = 2, Value1 = 2 });
                list1.Add(new tb_SensorRecordModel { ID = 3, Value1 = 3 });
                list1.Add(new tb_SensorRecordModel { ID = 4, Value1 = 4 });
                list1.Add(new tb_SensorRecordModel { ID = 5, Value1 = 5 });
    
                //构造新的一段数据
                List<tb_SensorRecordModel> list2 = new List<tb_SensorRecordModel>();
                list2.Add(new tb_SensorRecordModel { ID = 2, Value1 = 2222 });
                list2.Add(new tb_SensorRecordModel { ID = 3, Value1 = 3333 });
    
                //删除 旧的 那段数据
                list1.RemoveRange(1, 2);
    
                //将新的 这段数据 插入到 指定位置
                list1.InsertRange(1, list2);
    
                list1.ForEach(c =>
                {
                    Console.WriteLine($"{c.ID},{c.Value1}");
                });
                Console.Read();
                return;
  • 相关阅读:
    angular.isDefined()
    angular.isDate()
    angular.isArray()
    .NET中栈和堆的比较
    SQL Server 2012配置Always On可用性组
    一分钟了解负载均衡的一切
    C# 线程并发锁
    获取Http请求参数
    什么是WCF
    Bitmap算法应用
  • 原文地址:https://www.cnblogs.com/guxingy/p/11075864.html
Copyright © 2020-2023  润新知