リストから再取得したアイテムをフィルタします

9066 ワード

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;





namespace ConsoleApplication5

{

    class Program

    {

       

        staticvoid Main(string[] args)

        {

            List<item> list =new List<item>();



            list.Add(new item { item1 =1, item2 ="b", item3 =4.0,item4=8 });  

            list.Add(new item { item1 =1, item2 ="a", item3 =1.0 ,item4=9});

            list.Add(new item { item1 =2, item2 ="a", item3 =1.0 ,item4=7});

            list.Add(new item { item1 =3, item2 ="a", item3 =1.0 ,item4=6});

            list.Add(new item { item1 =1, item2 ="a", item3 =2.0 ,item4=5});



            



            List<item> temp = list.Distinct(new item_collection_DistinctBy_item1()).ToList();



            foreach (var item in temp)

            {

                Console.WriteLine(item);

            }

            Console.Read();

        }

    }



    class item

    {

        publicint item1

        { get; set; }



        publicstring item2

        { get; set; }



        publicdouble item3

        { get; set; }



        publicint item4

        { get; set; }



        publicoverridestring ToString()

        {

            returnstring.Format("item1:{0},item2:{1},item3:{2},item4:{3}", item1, item2, item3,item4);

        }

    }



    class item_collection_DistinctBy_item1 : IEqualityComparer<item>

    {

        

        publicbool Equals(item x, item y)

        {

            if (x.item1 == y.item1 && x.item3 == y.item3)

            {

                returntrue;

            }

            else

            {

                returnfalse;

            }

        }



        publicint GetHashCode(item obj)

        {

            return0;

        }

    }

  

}