Linq,确定列表是否相等 [英] Linq, determine if list are equals

查看:53
本文介绍了Linq,确定列表是否相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定List<List<int>>中的项是否相等?

How to determine, if items in a List<List<int>> are equals ?

List<List<int>> equals = new List<List<int>>()
{
    new List<int>() { 1,2 },
    new List<int>() { 1,2 }
};

List<List<int>> notEquals = new List<List<int>>()
{
    new List<int>() { 1,2 },
    new List<int>() { 2,500}
};

推荐答案

您需要将第一个列表与所有其他列表进行比较,可以使用SequenceEqual:

You need to compare the first list with all others, you can use SequenceEqual:

List<int> first = yourLists[0];
bool allEqual = yourLists.Skip(1).All(l => first.SequenceEqual(l));

由于All在第一个不相等列表上返回false,因此非常有效.

Since All returns false on the first unequal list this is pretty efficient.

这篇关于Linq,确定列表是否相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆