using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestConsoleApp
{
public class DuplicatesInLinq
{
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
}
public static void CheckDuplicates()
{
List<string> lstNames = new List<string>() { "A", "A", "B", "C" };
bool val = lstNames.GroupBy(n => n).Any(c => c.Count() > 1);
if (val)
Console.WriteLine("Duplicate character found in list");
List<int> lstNum = new List<int>() { 1, 2, 2, 3 };
bool val1 = lstNum.GroupBy(x => x).Any(c => c.Count() > 1);
if (val1)
Console.WriteLine("Duplicate number found in list");
List<Employee> lstEmployees = new List<Employee>();
lstEmployees.Add(new Employee() { Id = 1, Name = "E1" });
lstEmployees.Add(new Employee() { Id = 2, Name = "E2" });
lstEmployees.Add(new Employee() { Id = 3, Name = "E2" });
lstEmployees.Add(new Employee() { Id = 4, Name = "E4" });
bool val3 = lstEmployees.GroupBy(x => x.Name).Any(x1 => x1.Count() > 1);
if (val3)
Console.WriteLine("Duplicate employees found in list");
}
public static void Main()
{
CheckDuplicates();
}
}
}The code to check duplicates in a strongly type generic list.