1

In my project I'm having a critical problem: I have a collection of all Employees in a collection. Some of the Employees have the same LName:

public class Employee
{
    public int ID { get; set; }
    public string FName { get; set; }
    public string MName { get; set; }
    public string LName { get; set; }
    public DateTime DOB { get; set; }
    public char Gender { get; set; }
}

public class MyClass
{
    public List<Employee> GetAll()
    {
        List<Employee> empList = new List<Employee>();
        empList.Add(new Employee()
        {
            ID = 1,
            FName = "John",
            MName = "",
            LName = "Shields",
            DOB = DateTime.Parse("12/11/1971"),
            Gender = 'M'
        });
        empList.Add(new Employee()
        {
            ID = 2,
            FName = "Mary",
            MName = "Matthew",
            LName = "Jacobs",
            DOB = DateTime.Parse("01/17/1961"),
            Gender = 'F'
        });
        empList.Add(new Employee()
        {
            ID = 3,
            FName = "Amber",
            MName = "Carl",
            LName = "Agar",
            DOB = DateTime.Parse("12/23/1971"),
            Gender = 'M'
        });
        empList.Add(new Employee()
        {
            ID = 4,
            FName = "Kathy",
            MName = "",
            LName = "Foxsss",
            DOB = DateTime.Parse("11/15/1976"),
            Gender = 'F'
        });
        empList.Add(new Employee()
        {
            ID = 5,
            FName = "Lena",
            MName = "Ashco",
            LName = "Bilton",
            DOB = DateTime.Parse("05/11/1978"),
            Gender = 'F'
        });
        empList.Add(new Employee()
        {
            ID = 6,
            FName = "Susanne",
            MName = "",
            LName = "Buck",
            DOB = DateTime.Parse("03/7/1965"),
            Gender = 'F'
        });
        empList.Add(new Employee()
        {
            ID = 7,
            FName = "Jim",
            MName = "",
            LName = "Hooks",
            DOB = DateTime.Parse("09/11/1972"),
            Gender = 'M'
        });
        empList.Add(new Employee()
        {
            ID = 8,
            FName = "Jane",
            MName = "G",
            LName = "Hooks",
            DOB = DateTime.Parse("12/11/1972"),
            Gender = 'F'
        });
        empList.Add(new Employee()
        {
            ID = 9,
            FName = "Robert",
            MName = "",
            LName = "Fox",
            DOB = DateTime.Parse("06/28/1964"),
            Gender = 'M'
        });
        empList.Add(new Employee()
        {
            ID = 10,
            FName = "Cindy",
            MName = "Preston",
            LName = "Fox",
            DOB = DateTime.Parse("01/11/1978"),
            Gender = 'M'
        });

        return empList;
    }
}

How can I get the duplicate records (based on LName) from my collection using LINQ?

1
  • 1
    You should read about Collection Initializers. Also, when using object initializers, the parentheses are redundant :) Commented Jul 31, 2013 at 13:00

2 Answers 2

6

It's not clear, but looks like you're looking for following:

var duplicates = GetAll().GroupBy(x => x.LName)
                         .Where(g => g.Count() > 1)
                         .SelectMany(g => g)
                         .ToList()

It groups element by LName, takes only groups that have more than 1 element and returns them as a list.

duplicates will be List<Employee>.

You can make it a bit better using ToDictionary instead of ToList:

var duplicates = GetAll().GroupBy(x => x.LName)
                         .Where(g => g.Count() > 1)
                         .ToDictionary(g = > g.Key, g.ToList());

It will be Dictionary<string, List<Employee>>, with a LName as a dictionary key, and list of items with given LName as Value.

Sign up to request clarification or add additional context in comments.

3 Comments

Why not .ToLookup(g = > g.Key)? Seems a lot cleaner than .ToDictionary(g = > g.Key, g.ToList())
@p.s.w.g Yes, it could be a lookup :)
@p.s.w.g sorry for that but when i use .ToLookup(g = > g.Key) then it will give me distinct duplicate record but i wont all duplicate record not distinct.
1

You don't need to do a full count for a given key; you can actually be a little more efficient and still clearly express what it means for there to be a duplicate. To check for duplicates, you merely need to check that after you skip the first item, see if there are any remaining:

var duplicates = GetAll().GroupBy(x => x.LName)
                         .Where(g => g.Skip(1).Any())
                         .SelectMany(g => g);

You can convert this to a list if you need with ToList. Or you can say:

var duplicates = GetAll().GroupBy(x => x.LName)
                         .Where(g => g.Skip(1).Any())
                         .ToLookup(g => g.Key);

to have them collected by Employee.LName.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.