I want to catch duplicates mutually exclusively, that is, I need to show that both the first and the third items are duplicates, and that the first and the fourth items are duplicates.
public class Foo
{
public String Name { get; set; }
public String SName { get; set; }
}
class Program
{
static void Main(string[] args)
{
var list = new List<Foo>();
list.Add(new Foo { Name = "a", SName = "d" });
list.Add(new Foo { Name = "b", SName = "e" });
list.Add(new Foo { Name = "c", SName = "a" });
list.Add(new Foo { Name = "a", SName = "f" });
// only groups by 1 name
var duplicates = list.GroupBy(i => i.Name).Where(g => g.Count() > 1).Select(g => g.Key);
}
}
I know this can be done trivially with foreach, I want to learn.
So we’ll start out by getting all of the repeated name values from either column, this is fairly easy:
Next we take each of those names and find all of the items that contain that value. The end result is a sequence for each distinct name where the sequence is all of the
Fooitems containing that value.If you want it to be, instead of a sequence of Lists of Foos, a sequence of an item with both the distinct value and a sequence then it’s easy enough to add in: