如何使用linq获取所有重复记录

Raj*_*hit 1 linq asp.net

在我的项目中,我遇到了一个严重问题:我收集了所有Employees的集合.有些人Employee有相同的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

如何LName使用LINQ从我的集合中获取重复记录(基于)?

Mar*_*zek 6

目前尚不清楚,但看起来你正在寻找以下内容:

var duplicates = GetAll().GroupBy(x => x.LName)
                         .Where(g => g.Count() > 1)
                         .SelectMany(g => g)
                         .ToList()
Run Code Online (Sandbox Code Playgroud)

它对element by LName进行分组,仅包含具有多个元素的组,并将它们作为列表返回.

duplicates会的List<Employee>.

您可以使用ToDictionary而不是使用它来改善它ToList:

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

这将是Dictionary<string, List<Employee>>,与LName作为字典的键,并与给定的产品清单LNameValue.