list 去重复项

来源:互联网 发布:淘宝有哪些官方手办店 编辑:程序博客网 时间:2024/05/04 21:24
msdn
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Test{    public enum Id    {        Big = 1,        Middle = 2,        Small = 3    }    class Student    {        public Student(String name, int age)         {            this.Name = name;            this.Age = age;        }        public string Name{ get;set; }        public int Age{ get;set; }    }    class Program    {        static void Main(string[] args)        {            List<Id> list = new List<Id>{                Id.Big,                Id.Middle,                Id.Small,                Id.Middle,                Id.Big            };            List<Student> stuList = new List<Student>{                new Student("小明", 25),                new Student("小华", 35),                new Student("小明", 45),            };            var newList = list.Distinct();            var newStuList = (from p in stuList select p.Name).Distinct();            foreach (Id obj in newList)            {                Console.WriteLine(Convert.ToInt32(obj)+" --- "+obj.ToString());            }            foreach (string Name in newStuList)             {                Console.WriteLine(Name);            }            Console.Read();        }    }}