本文主要介绍.NET(C#)中,通过JSON字符串生成的对象列表中重复对象的去重的方法,以及相关的示例代码。

1、使用Linq中GroupBy去重

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using Newtonsoft.Json;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            
            List<People> pList = new List<People>();
            People p1 = new People(1, "C", 4);
            People p2 = new People(2, "Java", 7);
            People p3 = new People(3, "Python", 11);
            People p4 = new People(4, "Linux", 15);
            People p5 = new People(5, "CJavaPY", 1);
            People p6 = new People(3, "Python", 11);
            People p7 = new People(4, "Linux", 15);
            People p8 = new People(5, "CJavaPY", 1);
            pList.Add(p1);
            pList.Add(p2);
            pList.Add(p3);
            pList.Add(p4);
            pList.Add(p5);
            pList.Add(p6);
            pList.Add(p7);
            pList.Add(p8);
            //string json= JsonConvert.SerializeObject(pList);
            //Console.WriteLine(json);
            /*[{"Id":1,"Name":"C","Age":4},{"Id":2,"Name":"Java","Age":7},
            {"Id":3,"Name":"Python","Age":11},{"Id":4,"Name":"Linux","Age":15},
            {"Id":5,"Name":"CJavaPY","Age":1},{"Id":3,"Name":"Python","Age":11},
            {"Id":4,"Name":"Linux","Age":15},{"Id":5,"Name":"CJavaPY","Age":1}]
            */
            //pList = JsonConvert.DeserializeObject<List<People>>(json);
            var pList1 = pList.GroupBy(p => new { Id=p.Id,Age=p.Age,Name=p.Name });
            foreach (var item in pList1)
            {
                Console.Write(item.Key);
                foreach (var item1 in item)
                {
                    Console.Write(item1.Name);
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
    public class People
    {
        public People(int id, string name, int age)
        {
            this.Id = id;
            this.Name = name;
            this.Age = age;
        }
        public int Id
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
    }
}

2、使用Linq中Distinct去重

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using Newtonsoft.Json;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            
            List<People> pList = new List<People>();
            People p1 = new People(1, "C", 4);
            People p2 = new People(2, "Java", 7);
            People p3 = new People(3, "Python", 11);
            People p4 = new People(4, "Linux", 15);
            People p5 = new People(5, "CJavaPY", 1);
            People p6 = new People(3, "Python", 11);
            People p7 = new People(4, "Linux", 15);
            People p8 = new People(5, "CJavaPY", 1);
            pList.Add(p1);
            pList.Add(p2);
            pList.Add(p3);
            pList.Add(p4);
            pList.Add(p5);
            pList.Add(p6);
            pList.Add(p7);
            pList.Add(p8);
            //string json= JsonConvert.SerializeObject(pList);
            //Console.WriteLine(json);
            /*[{"Id":1,"Name":"C","Age":4},{"Id":2,"Name":"Java","Age":7},
            {"Id":3,"Name":"Python","Age":11},{"Id":4,"Name":"Linux","Age":15},
            {"Id":5,"Name":"CJavaPY","Age":1},{"Id":3,"Name":"Python","Age":11},
            {"Id":4,"Name":"Linux","Age":15},{"Id":5,"Name":"CJavaPY","Age":1}]
            */
            //pList = JsonConvert.DeserializeObject<List<People>>(json);
           // pList.Distinct();
            var pList1 = pList.Distinct(new PeopleComparer()); //pList.GroupBy(p => new { Id=p.Id,Age=p.Age,Name=p.Name });
            foreach (var item in pList1)
            {
                Console.Write(item.Name);
                //foreach (var item1 in item)
                //{
                //    Console.Write(item1.Name);
                //}
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
    public class PeopleComparer : IEqualityComparer<People>
    {
        public bool Equals(People x, People y)
        {
            if (x == null)
                return y == null;
            return x.Id == y.Id && x.Name == y.Name && x.Age == y.Age;
        }
        public int GetHashCode(People obj)
        {
            if (obj == null)
                return 0;
            return obj.Id.GetHashCode();
        }
    }
    public class People
    {
        public People(int id, string name, int age)
        {
            this.Id = id;
            this.Name = name;
            this.Age = age;
        }
        public int Id
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
    }
}

3、使用foreach循环去重

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using Newtonsoft.Json;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            
            List<People> pList = new List<People>();
            People p1 = new People(1, "C", 4);
            People p2 = new People(2, "Java", 7);
            People p3 = new People(3, "Python", 11);
            People p4 = new People(4, "Linux", 15);
            People p5 = new People(5, "CJavaPY", 1);
            People p6 = new People(3, "Python", 11);
            People p7 = new People(4, "Linux", 15);
            People p8 = new People(5, "CJavaPY", 1);
            pList.Add(p1);
            pList.Add(p2);
            pList.Add(p3);
            pList.Add(p4);
            pList.Add(p5);
            pList.Add(p6);
            pList.Add(p7);
            pList.Add(p8);
            //string json= JsonConvert.SerializeObject(pList);
            //Console.WriteLine(json);
            /*[{"Id":1,"Name":"C","Age":4},{"Id":2,"Name":"Java","Age":7},
            {"Id":3,"Name":"Python","Age":11},{"Id":4,"Name":"Linux","Age":15},
            {"Id":5,"Name":"CJavaPY","Age":1},{"Id":3,"Name":"Python","Age":11},
            {"Id":4,"Name":"Linux","Age":15},{"Id":5,"Name":"CJavaPY","Age":1}]
            */
            //pList = JsonConvert.DeserializeObject<List<People>>(json);
            var pList1 = new List<People>();
            foreach (var item in pList)
            {
                if (pList1.Where(w => w.Id == item.Id && w.Name == item.Name && w.Age == item.Age).Count() == 0)
                    pList1.Add(item);
            }
            foreach (var item in pList1)
            {
                Console.Write(item.Name);
                //foreach (var item1 in item)
                //{
                //    Console.Write(item1.Name);
                //}
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
  
    public class People
    {
        public People(int id, string name, int age)
        {
            this.Id = id;
            this.Name = name;
            this.Age = age;
        }
        public int Id
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
    }
}

推荐文档