使用C#/ LinQ删除列表中的重复字符串,但忽略大小写

时间:2018-11-07 05:03:35

标签: c# linq

带有列表:

  

“ A”,“ Bb”,“ C”,“ a”,“ d”,“ bb”

如何在不使用大小写的情况下使用LinQ删除重复项?

此处的输出应为:

  

“ C”,“ d”

3 个答案:

答案 0 :(得分:6)

尝试

  var result = testList
               .GroupBy(item => item, StringComparer.OrdinalIgnoreCase)
               .Where(g => g.Count() == 1)
               .Select(g => g.Key)
               .ToList();

答案 1 :(得分:2)

Use可以将GroupBy方法与IgnoreCase字符串比较器之一一起使用,然后仅选择由单个元素组成的那些组

<body>
  <div id="wrapper">
    <header>
      <h1 class="text-format">Car Reviewer</h1>
    </header>

    <nav class="text-format">
      <ul>
        <li> <a href="index.html">Home</a> </li>
        <li> <a href="reviews.html">Reviews</a> </li>
        <li> <a href="about.html">About</a> </li>
      </ul>
    </nav>

    <main>
      <h2 class="text-format">Featured Reviews</h2>

      <!-- MAKING OF THE FEATURED REVIEWS IMAGES -->
      <!-- BORDER OF FEATURED REVIEWS ON HOME PAGE -->
      <div class="feature-review-border">

        <figure>
          <!-- IMAGE CONTAINER -->
          <div class="feature-review-holder1">
            <img src="images/f150.png" alt="Ford F-150" class="home-image">

            <figcaption>"The hands free options are easy to use and I use the bluetooth connectivity to play music and make phone calls" - Bob
            </figcaption>
          </div>
          <!-- END 'FEATURE-REVIEW-HOLDER' -->

        </figure>

      </div>

答案 2 :(得分:0)

您可以将GroupBy()与StringComparer.CurrentCultureIgnoreCase选项一起使用,如下所示:

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();