class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>();
Student stu1 = new Student(17, 170);
students.Add(stu1);
Student stu2 = new Student(18, 180);
students.Add(stu2);
Student stu1 = new Student(17, 170);
students.Add(stu3);
Student stu1 = new Student(18, 170);
students.Add(stu3);
Student stu1 = new Student(19, 190);
students.Add(stu4);
Student stu1 = new Student(22, 186);
students.Add(stu4);
//请写一个方法,去除里面重复的数据,并且返回一个新的集合(去除重复后的集合<重复:年龄和身高都相同>)
Console.ReadLine();
}
}
class Student
{
public Student() { } public Student(int age, double stature)
{
this._age = age;
this._stature = stature;
} private int _age;
/// <summary>
/// 年龄
/// </summary>
public int Age
{
get { return _age; }
set { _age = value; }
} private double _stature;
/// <summary>
/// 身高
/// </summary>
public double Stature
{
get { return _stature; }
set { _stature = value; }
}
}

解决方案 »

  1.   

    http://topic.csdn.net/u/20100604/23/d0700f64-8a1b-4684-9127-d915ea38f50b.html
      

  2.   

    循环,加一个bool进行判断,已经存在就跳过
      

  3.   

    (1)这个叫拉姆表达式吗?
          List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 ,21,21};      IEnumerable<int> distinctAges = ages.Distinct();      this.Text = distinctAges.Count().ToString();(2)把你的元素加到Dictionary<int,int>中,加完后,里边的数据就是不重复数据了。 
      

  4.   

    使用自定义扩展方法
    给出完整代码:
    namespace WebApplication1
    {     static class Extensions
        {
            internal static bool stuEquals(this Student x, Student y)
            {
                if (x.Age == y.Age && x.Stature == y.Stature)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            internal static bool ContainsStu(this List<Student> list, Student stu)
            {
                bool isContains = false;
                foreach (Student s in list)
                {
                    if (s.stuEquals(stu))
                    {
                        isContains = true;
                        break;
                    }
                }
                return isContains;
            }
        }
        public class Student
        {
            public Student(int age, double stature)
            {
                this._age = age;
                this._stature = stature;
            }        private int _age;
            /// <summary>
            /// 年龄
            /// </summary>
            public int Age
            {
                get { return _age; }
                set { _age = value; }
            }        private double _stature;
            /// <summary>
            /// 身高
            /// </summary>
            public double Stature
            {
                get { return _stature; }
                set { _stature = value; }
            }    }
        public partial class CSSTest : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                List<Student> students = new List<Student>();
                Student stu1 = new Student(17, 170);
                students.Add(stu1);
                Student stu2 = new Student(18, 180);
                students.Add(stu2);
                Student stu3 = new Student(17, 170);
                students.Add(stu3);
                Student stu4 = new Student(18, 170);
                students.Add(stu4);
                Student stu5 = new Student(19, 190);
                students.Add(stu5);
                Student stu6 = new Student(22, 186);
                students.Add(stu6);            List<Student> Newstudents = new List<Student>();
                foreach (Student stu in students)
                {
                    if (!Newstudents.ContainsStu(stu))
                    {
                        Newstudents.Add(stu);
                    }
                }                   }
        }
    }
      

  5.   

    参考:  http://blog.csdn.net/q107770540/archive/2010/11/15/6010387.aspx
      

  6.   

    static IEnumerable<Student> eliminate(List<Student> ss)
            {
                for (int i = 0; i < ss.Count; i++)
                {
                    for (int j = i + 1; j < ss.Count; j++)
                    {
                        if ((ss[i].Age == ss[j].Age) && (ss[i].Stature == ss[j].Stature))
                        {
                            ss.RemoveAt(j);
                            j--;
                        }
                    }
                }
                return ss;
            }
      

  7.   

    那个this到了我这里就出错啊,删了,就没用了
      

  8.   


     static List<Student> GetDifferent(List<Student> ss)
            {
                List<Student> newStudent = new List<Student>();
                for (int i = 0; i < ss.Count; i++)
                {
                    if (!newStudent.Contains(ss[i]))
                    {
                        newStudent.Add(ss[i]);
                    }
                }
                return newStudent;
            }
      

  9.   

            class Test
            {
                public int i;
            }        class TestComparer<T> : IEqualityComparer<T>
                where T : Test
            {            public int GetHashCode(T obj)
                {
                    return obj.GetHashCode();
                }
                public bool Equals(T t1, T t2)
                {
                    return t1.i == t2.i;
                }
            }
    比较对象属性值
      

  10.   

    8楼的方法很好,建议楼主用那个我出个偷懒的方法 :ppublic class Student
    {
    public uint Age { get; set; }
    public double Stature { get; set; } public Student(uint age, double stature)
    {
    this.Age = age;
    this.Stature = stature;
    }
    }// Form_Load 测试代码
    List<Student> students = new List<Student>();
    students.Add(new Student(5, 5d));
    students.Add(new Student(51, 3d));
    students.Add(new Student(5, 5d));
    students.Add(new Student(5, 5d));
    students.Add(new Student(23, 5d));
    students.Add(new Student(43, 25d));List<Student> copy = new List<Student>();//// 默认 Contains 方法无法甄别重复项目
    //foreach(Student item in students)
    //{
    //    if (copy.Contains(item)) continue;
    //    else copy.Add(item);
    //}// 根据7楼的提议,弄了个偷懒的法子
    Dictionary<uint, double> dic = new Dictionary<uint, double>();foreach(Student student in students)
    {
    if(!dic.ContainsKey(student.Age))
    {
    dic.Add(student.Age, student.Stature);
    copy.Add(student);
    continue;
    }
    if(!dic.ContainsValue(student.Stature))
    {
    dic.Add(student.Age, student.Stature);
    copy.Add(student);
    continue;
    }
    }Console.WriteLine("Count: {0}", copy.Count);// 输出 Count: 4