C#学习笔记(七):List list = new List()

// 商品集合信息
            List list = new List()
            {
                new Product() {Id = 1,Name = "IPhone4",Price = 1999,Remark = "心动不如行动"},
                new Product() {Id = 2,Name = "IPhone4s",Price = 2999,Remark = "心动不如行动"},
                new Product() {Id = 3,Name = "IPhone5",Price = 3999,Remark = "心动不如行动"},
                new Product() {Id = 4,Name = "IPhone5s",Price = 4999,Remark = "心动不如行动"},
                new Product() {Id = 5,Name = "IPhone6",Price = 5999,Remark = "心动不如行动"},
                new Product() {Id = 6,Name = "IPhone6s",Price = 6999,Remark = "心动不如行动"},
                new Product() {Id = 7,Name = "IPhone7",Price = 7999,Remark = "心动不如行动"},
                new Product() {Id =8,Name = "IPhone7s",Price = 8999,Remark = "心动不如行动"},
                new Product() {Id = 9,Name = "IPhone7s Plus",Price = 9999,Remark = "心动不如行动"},

                // 除了ID其它字段重复数据
                new Product() {Id = 1,Name = "IPhone4",Price = 1999,Remark = "心动不如行动"},
                new Product() {Id = 10,Name = "IPhone4s",Price = 2999,Remark = "心动不如行动"},
                new Product() {Id = 11,Name = "IPhone5",Price = 3999,Remark = "心动不如行动"},
                new Product() {Id = 12,Name = "IPhone5s",Price =4999,Remark = "心动不如行动"},
                new Product() {Id = 13,Name = "IPhone6",Price = 5999,Remark = "心动不如行动"},
                new Product() {Id = 14,Name = "IPhone6s",Price = 6999,Remark = "心动不如行动"},

                // 完全重复数据
                 new Product() {Id = 1,Code = "123456789",Name = "IPhone4",Price = 1999,Remark = "心动不如行动"},
                 new Product() {Id = 1,Code = "123456789",Name = "IPhone4",Price = 1999,Remark = "心动不如行动"},
                 new Product() {Id = 1,Code = "123456789",Name = "IPhone4",Price = 1999,Remark = "心动不如行动"},
                 new Product() {Id = 1,Code = "123456789",Name = "IPhone4",Price = 1999,Remark = "心动不如行动"},
                 new Product() {Id = 1,Code = "123456789",Name = "IPhone4",Price = 1999,Remark = "心动不如行动"},
            };


            //// 1:根据条件筛选价格大于3000的商品信息
            var query = list.Where(p=>p.Price>3000).ToList();
            foreach (var item in query)
            {
                Console.WriteLine(item.Id+"--"+item.Code + "--" +item.Name + "--" +item.Price + "--" +item.Remark);
            //}


            //// 2-1:去除重复的数据(声明一个类继承:IEqualityComparer)
            var query = list.Distinct(new DistinctComparer());
            foreach (var item in query.ToList())
            {
               Console.WriteLine(item.Id + "--" + item.Code + "--" + item.Name + "--" + item.Price + "--" + item.Remark);
            }

            //// 2-2:利用HashSet去除重复数据
            var js = new List() { "1", "1", "2", "3" };
            HashSet hs = new HashSet(js); //此时已经去掉重复的数据保存在hashset中

            foreach (var item in hs)
            {
                Console.WriteLine(item);
            }

            //// 获取list集合总数
            var query = list.Count();
            Console.WriteLine(query);
             
            //// 新增数据
            list.Add(new Product() { Id = 10, Price = 123, Remark = "新增", Name = "IPhone8" });
            foreach (var item in list)
            {
                Console.WriteLine(item.Id + "--" + item.Code + "--" + item.Name + "--" + item.Price + "--" + item.Remark);
            }

            //// 新增数据
            list.AddRange(new List() {new Product() {Id = 20,Name = "AddRange方法",Price = 1111,Remark = "AddRange添加"} });
            foreach (var item in list)
            {
                Console.WriteLine(item.Id + "--" + item.Code + "--" + item.Name + "--" + item.Price + "--" + item.Remark);
            }

            //// 删除所有数据
            list.Clear();
            var query = list.Count();
            Console.WriteLine(query);

            //// 删除指定数据
            list.Remove(new Product() { Id = 14, Name = "IPhone6s", Price = 6999, Remark = "心动不如行动" });
            foreach (var item in list)
            {
                Console.WriteLine(item.Id + "--" + item.Code + "--" + item.Name + "--" + item.Price + "--" + item.Remark);
            }

            // ForEach 遍历集合
            list.ForEach(p =>
            {
                Console.WriteLine(p.Id + "--" + p.Code + "--" + p.Name + "--" + p.Price + "--" + p.Remark);

            });

 

你可能感兴趣的:(C#)