Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get duplicates from list case insensitive

List<string> testList = new List<string>();
testList.Add("A");
testList.Add("A");
testList.Add("C");
testList.Add("d");
testList.Add("D");

This query is case sensitive:

// Result: "A"
List<String> duplicates = testList.GroupBy(x => x)
                                  .Where(g => g.Count() > 1)
                                  .Select(g => g.Key)
                                  .ToList();

How would it look case insensitive? (Result: "A", "d")

like image 613
Pollitzer Avatar asked Dec 04 '22 23:12

Pollitzer


1 Answers

By using overloaded implementation of the GroupBy where you can provide the comparer required, e.g. StringComparer.OrdinalIgnoreCase:

  var result = testList
    .GroupBy(item => item, StringComparer.OrdinalIgnoreCase)
    .Where(g => g.Count() > 1)
    .Select(g => g.Key)
    .ToList();
like image 81
Dmitry Bychenko Avatar answered Dec 11 '22 16:12

Dmitry Bychenko