在我的項目中,我遇到了一個嚴重問題:我收集了一個集合中的所有Employee
s。一些Employee
S的具有相同LName
:如何使用linq獲取所有重複記錄
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
)?
您應該閱讀[Collection Initializers](http://msdn.microsoft.com/zh-cn/library/vstudio/bb384062.aspx)。另外,當使用對象初始值設定項時,括號是多餘的:) – khellang