在我的项目中,我遇到了一个严重问题:我收集了所有Employee
个集合。有些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;
}
}
如何使用LINQ从我的集合中获取重复记录(基于LName
)?
答案 0 :(得分:6)
目前尚不清楚,但看起来你正在寻找以下内容:
var duplicates = GetAll().GroupBy(x => x.LName)
.Where(g => g.Count() > 1)
.SelectMany(g => g)
.ToList()
它按LName
对元素进行分组,仅包含具有多个元素的组,并将它们作为列表返回。
duplicates
将为List<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>>
,LName
作为字典键,以及给定LName
作为Value
的商品列表。
答案 1 :(得分: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
收集它们。