私はC#とLinqの新機能です重複するレコードを削除するには1つの問題がありますDept
を持たない人物を重複して削除してください。以下は、従業員のリストにこの 出力例のようにlinqを使用してフィールドのリストから重複を削除
private static void Main()
{
List<Employee> empList = new List<Employee>();
empList.Add(new Employee() { ID = 1, Name = "John", Age=23, Dept='computer'});
empList.Add(new Employee() { ID = 2, Name = "Mary", Age=25, Dept='computer'});
empList.Add(new Employee() { ID = 3, Name = "Amber",Age=23, Dept=''});
empList.Add(new Employee() { ID = 4, Name = "Kathy",Age=25, Dept=''});
empList.Add(new Employee() { ID = 5, Name = "Lena", Age=27, Dept='computer'});
empList.Add(new Employee() { ID = 6, Name = "John", Age=28, Dept=''});
empList.Add(new Employee() { ID = 7, Name = "Kathy",Age=27, Dept='Tester'});
empList.Add(new Employee() { ID = 8, Name = "John", Age=23, Dept='computer'});
var dup = empList
.GroupBy(x => new { x.FName })
.Select(group => new { Name = group.Key, Count = group.Count() })
.OrderByDescending(x => x.Count);
foreach (var x in dup)
{
Response.Write(x.Count + " " + x.Name);
}
}
class Employee
{
public int ID { get; set; }
public string FName { get; set; }
public int Age { get; set; }
public char Dept { get; set; }
}
最終出力の外観を使用した簡単な例である: -
empList.Add(new Employee() { ID = 1, Name = "John", Age=23, Dept='computer'});
empList.Add(new Employee() { ID = 2, Name = "Mary", Age=25, Dept='computer'});
empList.Add(new Employee() { ID = 3, Name = "Amber",Age=23, Dept=''});
empList.Add(new Employee() { ID = 5, Name = "Lena", Age=27, Dept='computer'});
empList.Add(new Employee() { ID = 7, Name = "Kathy",Age=27, Dept='Tester'});
empList.Add(new Employee() { ID = 8, Name = "John", Age=23, Dept='computer'});
私はそれらの重複したレコードを削除する必要がDEPTを持っていけない人たち。
条件1 重複レコードは複数回出ますが、削除する必要のあるdeptを持たない1つのレコードです。そして残りのレコードが(Dept
が文字列のようです)IDが一意であるので、あなたがこのアプローチを使用することができ、出力
重複との両方があるのであれば 'Dept'は、あなたは両方を維持するだろうか? – juharr
同じ名前の重複を検討していますか? – krillgar
そして、部署のない非重複従業員はどうですか? –