Remove duplicates from a List in C#

前端 未结 27 1810
广开言路
广开言路 2020-11-22 04:41

Anyone have a quick method for de-duplicating a generic List in C#?

相关标签:
27条回答
  • 2020-11-22 05:28

    Another way in .Net 2.0

        static void Main(string[] args)
        {
            List<string> alpha = new List<string>();
    
            for(char a = 'a'; a <= 'd'; a++)
            {
                alpha.Add(a.ToString());
                alpha.Add(a.ToString());
            }
    
            Console.WriteLine("Data :");
            alpha.ForEach(delegate(string t) { Console.WriteLine(t); });
    
            alpha.ForEach(delegate (string v)
                              {
                                  if (alpha.FindAll(delegate(string t) { return t == v; }).Count > 1)
                                      alpha.Remove(v);
                              });
    
            Console.WriteLine("Unique Result :");
            alpha.ForEach(delegate(string t) { Console.WriteLine(t);});
            Console.ReadKey();
        }
    
    0 讨论(0)
  • 2020-11-22 05:28

    I think the simplest way is:

    Create a new list and add unique item.

    Example:

            class MyList{
        int id;
        string date;
        string email;
        }
        
        List<MyList> ml = new Mylist();
    
    ml.Add(new MyList(){
    id = 1;
    date = "2020/09/06";
    email = "zarezadeh@gmailcom"
    });
    
    ml.Add(new MyList(){
    id = 2;
    date = "2020/09/01";
    email = "zarezadeh@gmailcom"
    });
    
     List<MyList> New_ml = new Mylist();
    
    foreach (var item in ml)
                    {
                        if (New_ml.Where(w => w.email == item.email).SingleOrDefault() == null)
                        {
                            New_ml.Add(new MyList()
                            {
                              id = item.id,
         date = item.date,
                   email = item.email
                            });
                        }
                    }
    
    0 讨论(0)
  • 2020-11-22 05:29

    How about:

    var noDupes = list.Distinct().ToList();
    

    In .net 3.5?

    0 讨论(0)
  • 2020-11-22 05:30

    You can use Union

    obj2 = obj1.Union(obj1).ToList();
    
    0 讨论(0)
  • 2020-11-22 05:31

    I like to use this command:

    List<Store> myStoreList = Service.GetStoreListbyProvince(provinceId)
                                                     .GroupBy(s => s.City)
                                                     .Select(grp => grp.FirstOrDefault())
                                                     .OrderBy(s => s.City)
                                                     .ToList();
    

    I have these fields in my list: Id, StoreName, City, PostalCode I wanted to show list of cities in a dropdown which has duplicate values. solution: Group by city then pick the first one for the list.

    I hope it helps :)

    0 讨论(0)
  • 2020-11-22 05:32

    If you're using .Net 3+, you can use Linq.

    List<T> withDupes = LoadSomeData();
    List<T> noDupes = withDupes.Distinct().ToList();
    
    0 讨论(0)
提交回复
热议问题