简体   繁体   English

比较清单 <sting> 与foreach循环忽略大小写c#

[英]Compare list<sting> with foreach loop ignoring case c#

I have an application can compare two lsts of data the user inputs. 我有一个应用程序可以比较用户输入的两个数据。 Currently however, if two values are the same but one is upper case the application classes this as not a match. 但是,当前,如果两个值相同但一个为大写,则应用程序将此分类为不匹配。

Now I have used StringComparison.InvariantCultureIgnoreCase before but I am unsure of how to add this within a foreach loop. 现在,我以前使用过StringComparison.InvariantCultureIgnoreCase ,但是我不确定如何在foreach循环中添加它。

My current code is as follows; 我当前的代码如下:

List<string> mylistA = new List<string>();
List<string> mylistB = new List<string>();

if (listATextBox.LineCount > 0) 
    for (int i = 1; i <= listATextBox.LineCount; i++) 
        mylistA.Add(listATextBox.GetLineText(i - 1).Replace(Environment.NewLine, "").Trim());

if (listBTextBox.LineCount > 0) 
    for (int i = 1; i <= listBTextBox.LineCount; i++) 
        mylistB.Add(listBTextBox.GetLineText(i - 1).Replace(Environment.NewLine, "").Trim());

foreach (string line in mylistA)
{             
    if (mylistB.Contains(line))
    {
            ResultsToDocument();
    }
}

You can use Enumerable.Contains with this comparer as parameter instead of List.Contains : 您可以将Enumerable.Contains与此比较器一起用作参数,而不是List.Contains

foreach (string line in mylistA)
{
    if (mylistB.Contains(line, StringComparer.InvariantCultureIgnoreCase))
    {
        ResultsToDocument();
    }
}

A more efficient approach would be Intersect which also has this overload: 一种更有效的方法是Intersect ,它也具有以下重载:

foreach(string line in mylistA.Intersect(mylistB, StringComparer.InvariantCultureIgnoreCase))
     ResultsToDocument(); // does this make sense, not passing the line?

Try this: 尝试这个:

foreach (string line in mylistA)
{
    if (mylistB.Any(b => b.Equals(line, StringComparison.OrdinalIgnoreCase)))
    {
        ResultsToDocument();
    }
}

For better performance and readability you can use Intersect 为了获得更好的性能和可读性,您可以使用Intersect

foreach (var element in mylistA.Intersect(mylistB, StringComparer.OrdinalIgnoreCase))
   ResultsToDocument();

If there are duplicates in mylistA that needs to be preserved. 如果mylistA中有重复mylistA需要保留。 Using a hashset for the lookup would be the right way to go and would have the same performance as the intersect solution. 使用哈希集进行查找将是正确的方法,并且具有与相交解决方案相同的性能。

var set = new HashSet<string>(mylistB, StringComparer.OrdinalIgnoreCase)
foreach (string line in mylistA)
{             
    if (set.Contains(line))
    {
         ResultsToDocument();
    }
}

I'd go with: 我会去:

    foreach (string lineA in mylistA)
    {     
       foreach (string lineB in mylistB)
       {                     
            if (lineB.ToUpper() == lineA.ToUpper())
            {
                  ResultsToDocument();
            }
       }
    }

but there are probably better solutions to achieve the same result 但是可能有更好的解决方案可以达到相同的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM