如何比较清单 [英] how to compare lists
本文介绍了如何比较清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用linq让我知道如何比较2个字符串列表..我只想知道它们是否相等.这两个列表具有相同的值,可以使用Except运算符并检查结果的计数是否为零(如Tarun所指出的,您需要检查两种方法是否相同),就像这样
列表< string>一个= 新列表< string>(){" , b", c"};
List< string>两个= 新列表< string>(){" , b", c", d"};
// 如果两个值都相同
if (one.Except(two).Count()== 0 &&两个. (一个).Count()== 0 )
{
MessageBox.Show(" );
}
其他
{
MessageBox.Show(" );
}
// 忽略大小写
如果(一个.Except(两个,StringComparer.CurrentCultureIgnoreCase).Count()== 0 && ; two.Except(one,StringComparer.CurrentCultureIgnoreCase).Count()== 0 )
{
MessageBox.Show(" );
}
其他
{
MessageBox.Show(" );
}
// 如果一个引用与两个引用相同
如果(一个等于(两个))
{
MessageBox.Show(" );
}
其他
{
MessageBox.Show(" );
}
希望这对您有帮助
尝试:
列表< string> ListA = 新 List< string>(" .Split('))); List< string> ListB = 新列表< string>(" .Split(' '))); var 差异= ListA.Where(x = > !ListB.Any(x1 = > x1 == x)) .Union(ListB.Where(x = > !ListA.Any(x1 = > x1 = = x))); 如果(differences.Count()!= 0 ) { Console.WriteLine(" ); }
static void Main(string[] args) { List<string> List1 = new List<string> { "Car", "Home", "City" }; List<string> List2 = new List<string> { "Car", "Home", "City" }; List<string> List3 = new List<string> { "Home", "Car", "City" }; Console.WriteLine(List1.SequenceEqual(List2));//Result is True Console.WriteLine(List1.SequenceEqual(List3));//Result is False Console.Read(); }
Using linq let me know how to compare 2 list of strings.. i oly want to know whether they are equal
解决方案
By equal I expect you mean whether the two lists have the same values, you can use the Except operator and check whether the result has a count of zero (as pointed out by Tarun, you need to check whether both ways are the same ), something like this
List<string> one = new List<string>() { "a", "b", "c" }; List<string> two = new List<string>() { "a", "b", "c", "d" }; //If both have same values if (one.Except(two).Count() == 0 && two.Except(one).Count() == 0) { MessageBox.Show("They have same values"); } else { MessageBox.Show("They are not same."); } //To ignore case if (one.Except(two, StringComparer.CurrentCultureIgnoreCase).Count() == 0 && two.Except(one, StringComparer.CurrentCultureIgnoreCase).Count() == 0) { MessageBox.Show("They have same values, igoring case"); } else { MessageBox.Show("They are not same."); } //If one is same reference as two if (one.Equals(two)) { MessageBox.Show("They are the same reference"); } else { MessageBox.Show("They are not same reference"); }
Hope this helps
Try:
List<string> ListA = new List<string>("hello paul this is a text string".Split(' ')); List<string> ListB = new List<string>("hello paul this is a text string again".Split(' ')); var differences = ListA.Where(x => !ListB.Any(x1 => x1 == x)) .Union(ListB.Where(x => !ListA.Any(x1 => x1 == x))); if (differences.Count() != 0) { Console.WriteLine("Different!"); }
static void Main(string[] args) { List<string> List1 = new List<string> { "Car", "Home", "City" }; List<string> List2 = new List<string> { "Car", "Home", "City" }; List<string> List3 = new List<string> { "Home", "Car", "City" }; Console.WriteLine(List1.SequenceEqual(List2));//Result is True Console.WriteLine(List1.SequenceEqual(List3));//Result is False Console.Read(); }
这篇关于如何比较清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文