0

With the list:

"A", "Bb", "C", "a", "d", "bb"

How can I use LinQ to remove duplicates while ignoring casing?

the output here should be:

"C", "d"

Thomas
  • 10,933
  • 14
  • 65
  • 136
  • 2
    Possible duplicate of [LINQ Distinct operator, ignore case?](https://stackoverflow.com/questions/283063/linq-distinct-operator-ignore-case) – D-Shih Nov 07 '18 at 05:27
  • yes, I think you're right. And the solution there is shorter too. – Thomas Nov 07 '18 at 12:28

3 Answers3

7

try this

  var result = testList
               .GroupBy(item => item, StringComparer.OrdinalIgnoreCase)
               .Where(g => g.Count() == 1)
               .Select(g => g.Key)
               .ToList();
Sooriya Dasanayake
  • 1,106
  • 1
  • 7
  • 14
2

Use can use the GroupBy method with one of the IgnoreCase string comparers, then select only those groups consisting of a single element

.GroupBy(_ => _, StringComparer.CurrentCultureIgnoreCase)
.Where(_ => _.Count() == 1)
Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36
0

You can use GroupBy() with the StringComparer.CurrentCultureIgnoreCase option as follows:

List<String> data = new List<String>() { "A", "Bb", "C", "a", "d", "bb" };
List<String> newData = data.GroupBy(x => x, StringComparer.CurrentCultureIgnoreCase)
    .Where(el => el.Count() == 1)
    .Select(el => el.Key)
    .ToList();