1

在我的项目中,我遇到了一个关键问题:我有一个集合中所有Employees 的集合。一些Employees 具有相同的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;
    }
}

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

4

2 回答 2

6

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

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

它按 对元素进行LName分组,只接受具有超过 1 个元素的组并将它们作为列表返回。

duplicatesList<Employee>

ToDictionary您可以使用而不是使它更好一点ToList

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

它将是Dictionary<string, List<Employee>>,以 aLName作为字典键,并以给定的项目列表LName作为Value

于 2013-07-31T12:52:02.013 回答
1

您不需要对给定的键进行完整计数;您实际上可以提高一点效率,并且仍然清楚地表达存在重复的含义。要检查重复项,您只需要在跳过第一项后检查是否有剩余

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

如果需要,您可以将其转换为列表ToList。或者你可以说:

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

让他们收集Employee.LName

于 2013-07-31T13:01:27.383 回答