如何在目标表中找到重复的客户详细信息?

时间:2010-07-22 11:59:08

标签: c# .net visual-studio linq

我在Target类列表中重复了数据

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

我需要来自li2的来自ID的数据我该怎么做?例如ıD= 2重复像ID = 3i想要在li2中采取2,3(ID)细节?我怎么能用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; }

    }
}

1 个答案:

答案 0 :(得分:1)

据我所知,你的帖子,你试图找到基于选择一些属性的重复元素?如果是这样,LINQ中没有内置任何序列。你需要自己动手。这是你可以使用的东西:

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;
            }
        }
    }
}

然后你就这样使用它:

// 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);
    }

}