1

How can I find duplicate items in list based on particular value and group the duplicated items together?

If I have items that have same email address, I want them to be collected as duplicates because I use email address as "primary key" in the collection. Not all values are same in these items.

For example:

var numberOfTestcasesWithDuplicates = 
                                 Customers.GroupBy(x => x.emailaddress).ToList();

Would give me a collection of duplicated items, but then I want to collect the duplicated items into grouped collections where I can managed these items and see what items are duplicated?

Thank you

4

2 回答 2

6

也许您只想要包含多个项目的组?

var numberOfTestcasesWithDuplicates = Customers.GroupBy(x => x.emailaddress)
                                               .Where(x => x.Count() > 1)
                                               .ToList();

.Where(p => p.Count() > 1)检查组成组的项目数。

于 2013-08-14T14:37:35.403 回答
0

如何根据特定值在列表中查找重复项并将重复项分组在一起?

听起来像GroupBy我。您已经在问题的代码中完成了分组 - 您只需要使用结果。的结果GroupBy是一个组序列,其中每个组是一个键和一个共享该键的值序列。例如:

foreach (var group in Customers.GroupBy(x => x.emailaddress))
{
    Console.WriteLine("Customers with email address {0}", group.Key);
    foreach (var customer in group)
    {
        Console.WriteLine("  {0}", customer.Name); // Or whatever
    }
}
于 2013-08-14T14:34:48.637 回答