using System.Diagnostics;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading.Tasks;
public static void Main()
var setting = new Faker<EzEmployee>()
.RuleFor(p => p.RowId, f => Guid.NewGuid().ToString())
.RuleFor(p => p.EmailAddress, (f, p) => f.Internet.Email())
.RuleFor(p => p.Status, f => f.PickRandom<EzStatus>().ToString());
var emps = setting.Generate(15000);
Console.WriteLine("Total Distinct Email Count:" + emps.Select(x => x.EmailAddress).Distinct().Count());
Console.WriteLine(String.Empty);
public static void MyGoForeachList(List<EzEmployee> data)
var watch = new Stopwatch();
var sortedData = data.Where(x => x.Status.Equals("ACTIVE", StringComparison.CurrentCultureIgnoreCase) || x.Status.Equals("INACTIVE", StringComparison.CurrentCultureIgnoreCase)).OrderBy(x => x.EmailAddress).ThenByDescending(x => x.Status.Equals("ACTIVE", StringComparison.CurrentCultureIgnoreCase));
var result = new List<EzEmployee>();
foreach (var employee in sortedData)
if (!employee.EmailAddress.Equals(currentEmail, StringComparison.CurrentCultureIgnoreCase))
currentEmail = employee.EmailAddress;
Console.WriteLine("========= My Foreach List =========");
Console.WriteLine("Exec total Milliseconds:" + watch.ElapsedMilliseconds);
Console.WriteLine("Exec total count:" + result.Count());
public static void GoForeachList(List<EzEmployee> data)
var watch = new Stopwatch();
var result = new List<EzEmployee>();
var emails = data.Select(x => x.EmailAddress).Distinct().ToList();
foreach (var email in emails)
var activeEntity = data.FirstOrDefault(x => x.EmailAddress == email && x.Status.Equals("ACTIVE", StringComparison.CurrentCultureIgnoreCase));
if (activeEntity != null)
result.Add(activeEntity);
var inActiveEntity = data.FirstOrDefault(x => x.EmailAddress == email && x.Status.Equals("INACTIVE", StringComparison.CurrentCultureIgnoreCase));
if (inActiveEntity != null)
result.Add(inActiveEntity);
Console.WriteLine("========= Foreach List =========");
Console.WriteLine("Exec total Milliseconds:" + watch.ElapsedMilliseconds);
Console.WriteLine("Exec total count:" + result.Count());
public int Id { get; set; }
public string RowId { get; set; }
public string EmailAddress { get; set; }
public string Status { get; set; }