【问题标题】:c# find duplicates in list of string where the case doesn't matchc#在大小写不匹配的字符串列表中查找重复项
【发布时间】:2021-12-16 11:02:58
【问题描述】:

我有一个字符串列表 (List<string>),我只想获取 重复项,其中字符串的 cases 不匹配 /em>。当前查询给出了所有重复项。

在下面的示例中,我只想要"Chassis",因为仅此而已,有一个"chassis" 并且情况不同。请帮忙。

 private void btnSearchAndFlag_Click(object sender, RoutedEventArgs e)
 {
     List<string> stList = new List<string>() { 
       "Chassis", "chassis", "ABC", "ABC", "Chassis"  };
       
     var duplicates = stList
       .GroupBy(x => x, StringComparer.Ordinal)
       .Where(g => g.Count() > 1)
       .Select(y => y.Key)
       .ToList();
 }

【问题讨论】:

  • StringComparer.OrdinalIgnoreCase?

标签: c# linq duplicates


【解决方案1】:

您可以尝试对初始查询进行小修改:

  List<string> stList = new List<string>() { 
    "Chassis", "chassis", "ABC", "ABC", "Chassis" };
 
  var duplicates = stList
    .GroupBy(item => item, StringComparer.OrdinalIgnoreCase)
    .Where(group => group.Distinct(StringComparer.Ordinal).Count() > 1)
    .Select(group => group.Key)
    .ToList();

【讨论】:

  • 感谢工作。我会接受答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-16
  • 2015-10-02
  • 1970-01-01
  • 1970-01-01
  • 2013-03-19
  • 1970-01-01
  • 2012-01-19
相关资源
最近更新 更多