Dowemo


Question:

i have repeated data in Target class list

 new TargetClass{ TID=102, ID=2, Adress="afff", Zip="222"},


                new TargetClass{ TID=103, ID=2, Adress="bfff", Zip="222"},

i need reated ID's Data from li2 how can i do that? for example ıD=2 is repeated like ID = 3i want to take 2,3 (ID) detail in li2? How can i do that with linq?

code>namespace TestEng2


{


    public partial class Form1 : Form


    {


        public Form1()


        {


            InitializeComponent();


        }



        private void Form1_Load(object sender, EventArgs e)


        {


            List<MyClass> li1 = new List<MyClass>() { 


           new MyClass { ID = 1, Name = "yusuf", SurName = "karatoprak" },


           new MyClass{ ID =2, Name ="ali", SurName="kara"  },


           new MyClass{ ID =3, Name ="Oliver", SurName="gdfgfd"  },


           new MyClass{ ID =4, Name ="Khan", SurName="kargfga"  },


           new MyClass{ ID =5, Name ="Maradona", SurName="kagdgra"  }


            };


            List<TargetClass> li2 = new List<TargetClass>() { 


                new TargetClass{ TID=101, ID=1, Adress="fff", Zip="222"},


                new TargetClass{ TID=102, ID=2, Adress="afff", Zip="222"},


                new TargetClass{ TID=103, ID=2, Adress="bfff", Zip="222"},


                new TargetClass{ TID=104, ID=2, Adress="cfff", Zip="222"},


                new TargetClass{ TID=105, ID=3, Adress="dfff", Zip="222"},


                new TargetClass{ TID=106, ID=3, Adress="efff", Zip="222"},


                new TargetClass{ TID=107, ID=3, Adress="ffff", Zip="222"},


                new TargetClass{ TID=108, ID=2, Adress="hfff", Zip="222"},


                new TargetClass{ TID=109, ID=4, Adress="jfff", Zip="222"},


                new TargetClass{ TID=110, ID=5, Adress="kfff", Zip="222"}};



        }


    }



    public class MyClass


    {


        public int ID { get; set; }


        public String Name { get; set; }


        public String SurName { get; set; }


    }



    public class TargetClass


    {


        public int TID { get; set; }


        public int ID { get; set; }


        public String Adress { get; set; }


        public String Zip { get; set; }



    }


}



Best Answer:


From what I understand from your post, you're trying to find repeating elements based on selecting some property? If so, there's nothing built into LINQ that would do sequences. You'd need to roll your own. Here's something that you could use:

public static IEnumerable<TSource> Repeating<TSource, TValue>(this IEnumerable<TSource> source, Func<TSource, TValue> selector)


{


    using (var iterator = source.GetEnumerator())


    {


        if (iterator.MoveNext())


        {


            TValue prev = selector(iterator.Current);


            var comparer = EqualityComparer<TValue>.Default;



            while (iterator.MoveNext())


            {


                TValue curr = selector(iterator.Current);



                if (comparer.Equals(prev, curr))


                {


                    yield return curr;


                }


                prev = curr;


            }


        }


    }


}


You'd then use it like this:

// Sample class



class Person


{


    public int Id { get; set; }


    public int RefId { get; set; }


    public string Name { get; set; }


}



// Then in the program...



public static void Main(string[] args)


{



    List<Person> people = new List<Person>


    {


        new Person(){Id=1, RefId=1 Name="ABC"},


        new Person(){Id=2, RefId=1 Name="XYZ"},


        new Person(){Id=3, RefId=2 Name="abc"},


        new Person(){Id=4, RefId=3 Name="xyz"},


        new Person(){Id=5, RefId=3 Name="test2"}


    };



    foreach (Person repeated in people.Repeating(p => p.ID))


    {


        Console.WriteLine(repeated.ID);


    }



}





Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs