日期:2014-05-18 浏览次数:21250 次
public static bool isEqual(List<string> list1, List<string> list2)
{
list1.Sort();
list2.Sort();
if (list1.Count == list2.Count)
{
int i = 0;
foreach (string str in list1)
{
if (str != list2[i])
{
return false;
}
i++;
}
return true;
}
return false;
}
------解决方案--------------------
if (list1.Count == list2.Count && list1.Except(list2).Count() == 0 && list2.Except(list1).Count() == 0)
{
//集合相等
}
//或者
if (list1.Count == list2.Count && list1.All(x => list2.Contains(x)) && list2.All(x => list1.Contains(x)))
{
//集合相等
}
------解决方案--------------------
方法真的很多,比如
list1.Count == list2.Count && list1.OrderBy(x => x).Zip(list2.OrderBy(x => x), (x, y) => x == y).All(x => x == true);
------解决方案--------------------
需要先排序,否则如果集合中有重复的元素很多方法就不灵了。三楼稳妥,五楼也对。
------解决方案--------------------
protected void Page_Load(object sender, EventArgs e)
{
List<string> listA = new List<string>() { "0", "1" };
List<string> listB = new List<string>() { "1", "0" };
int count = listA.Except(listB).ToList().Count;//这一句就够了
if (count == 0)
{
Response.Write("相等"); return;
}
Response.Write("不相等");
}
------解决方案--------------------