我有一个课程如下:
public class Tag {
public Int32 Id { get; set; }
public String Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有两个标签列表:
List<Tag> tags1;
List<Tag> tags2;
Run Code Online (Sandbox Code Playgroud)
我使用LINQ的select来获取每个标签列表的ID.然后:
List<Int32> ids1 = new List<Int32> { 1, 2, 3, 4 };
List<Int32> ids2 = new List<Int32> { 1, 2, 3, 4 };
List<Int32> ids3 = new List<Int32> { 2, 1, 3, 4 };
List<Int32> ids4 = new List<Int32> { 1, 2, 3, 5 };
List<Int32> ids5 = new List<Int32> { 1, 1, …Run Code Online (Sandbox Code Playgroud) 我想比较两个集合(在C#中),但我不确定有效实现它的最佳方法.
我已经阅读了关于Enumerable.SequenceEqual的其他帖子,但这并不是我正在寻找的.
在我的情况下,如果它们都包含相同的项目(无论顺序),则两个集合将是相等的.
例:
collection1 = {1, 2, 3, 4};
collection2 = {2, 4, 1, 3};
collection1 == collection2; // true
Run Code Online (Sandbox Code Playgroud)
我通常做的是遍历一个集合中的每个项目,看看它是否存在于另一个集合中,然后循环遍历另一个集合的每个项目,看它是否存在于第一个集合中.(我首先比较长度).
if (collection1.Count != collection2.Count)
return false; // the collections are not equal
foreach (Item item in collection1)
{
if (!collection2.Contains(item))
return false; // the collections are not equal
}
foreach (Item item in collection2)
{
if (!collection1.Contains(item))
return false; // the collections are not equal
}
return true; // the collections are equal
Run Code Online (Sandbox Code Playgroud)
但是,这并不完全正确,并且它可能不是比较两个集合的最有效方法.
我能想到的一个例子是错误的:
collection1 …Run Code Online (Sandbox Code Playgroud) 我想比较我正在制作的程序的两个列表的值.我希望它将List 1的第一个值与List 2的第一个值进行比较,然后将List 1的第二个值与List 2的第二个值进行比较,依此类推.
我将如何在C#中执行此操作?
假设我有一个像这样的元组列表:
List<Tuple<string, string>> conflicts = new List<Tuple<string, string>>();
conflicts.Add(new Tuple<string, string>("Maths", "English"));
conflicts.Add(new Tuple<string, string>("Science", "French"));
conflicts.Add(new Tuple<string, string>("French", "Science"));
conflicts.Add(new Tuple<string, string>("English", "Maths"));
Run Code Online (Sandbox Code Playgroud)
我想检查元组列表的反向重复并删除它们,我将如何使用循环执行此操作?
注意:通过反向复制,我的意思是"英语","数学"和"数学","英语"的再次出现
注意:我的代码中的我的元组是使用SqlDataReader填充的,但我上面使用的示例非常接近它的布局方式.
这看起来很简单,但整晚都难倒
我正在尝试比较包含自定义对象的 2 个列表(包装在一个对象中)。我不关心顺序,但是如果列表 1 包含“1,2,3,4”,那么列表 2 必须并且只包含这些元素。例如:“4,2,3,1”
基于比较两个 List<T> 对象的相等性,忽略顺序 ignoring-order 我使用了 except 和 Any 但它没有给我想要的结果。
如果我使用Assert.Equals它失败,但Assert.IsTry(list1.equals(list2))成功。
此外,如果我删除 Equals 和 GetHashCode 实现,则两个测试都会失败。
public class AppointmentCollection : List<Appointment>
{
public override bool Equals(object obj)
{
var appCol = obj as AppointmentCollection;
if (appCol == null)
{
return false;
}
return (appCol.Count == this.Count) && !(this.Except(appCol).Any());
}
public override int GetHashCode()
{
unchecked
{
//use 2 primes
int hash = 17;
foreach (var appointment in …Run Code Online (Sandbox Code Playgroud) 我有四个字符串,如下所示.虽然它们具有不同的字符顺序和逗号后的不同间距 - 它们被认为具有same business value.
Enumerable.SequenceEqual?注意:"A,B"将被视为与"B,A,B,A,B"相同
注:我使用Visual Studio 2010同.Net Framework 4
码
string firstString = "A,B,C";
string secondString = "C,A,B";
string thirdString = "A,B, C";
string fourthString = "C, A,B";
//Set 1 Test
List<string> firstList = new List<string>(firstString.Split(','));
List<string> secondLsit = new List<string>(secondString.Split(','));
bool isStringsSame = Enumerable.SequenceEqual(firstList.OrderBy(t => t), secondLsit.OrderBy(t => t));
Console.WriteLine(isStringsSame);
//Set 2 Test
List<string> thirdList = new List<string>(thirdString.Split(','));
List<string> fourthList = new List<string>(fourthString.Split(','));
bool isOtherStringsSame = Enumerable.SequenceEqual(thirdList.OrderBy(t => …Run Code Online (Sandbox Code Playgroud) 我有两个列表要比较:
我想比较列表 A 和列表 B,如果列表 A 中存在列表 B 中的任何日期,则返回 true。
例如,如果 14-01-2020(在列表 B 中)出现在列表 A(肯定存在)中,那么它应该返回 true。
怎么做?
请注意:列表 A中的数据包含整个月的日期,而列表 B中仅包含几个日期。
我有3个方法应该返回相同的数据.数据是一个列表,MyObjectModel我想检查三个列表是否包含相同的数据.我想到在json中序列化3个列表中的每个列表并计算字符串的长度是否都相同.
有更好的方法吗?
谢谢.