相关疑难解决方法(0)

检查两个列表是否相等

我有一个课程如下:

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# linq

156
推荐指数
3
解决办法
20万
查看次数

比较两个集合的相等性,而不管它们中的项目顺序如何

我想比较两个集合(在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)

.net collections comparison equality

154
推荐指数
9
解决办法
9万
查看次数

比较2个列表的值C#

我想比较我正在制作的程序的两个列表的值.我希望它将List 1的第一个值与List 2的第一个值进行比较,然后将List 1的第二个值与List 2的第二个值进行比较,依此类推.

我将如何在C#中执行此操作?

c# compare list

7
推荐指数
1
解决办法
2343
查看次数

如何删除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填充的,但我上面使用的示例非常接近它的布局方式.

这看起来很简单,但整晚都难倒

c# asp.net tuples

3
推荐指数
1
解决办法
658
查看次数

C# 将列表与自定义对象进行比较但忽略顺序

我正在尝试比较包含自定义对象的 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)

c# collections comparison nunit assert

2
推荐指数
1
解决办法
4725
查看次数

检查两个逗号分隔的字符串是否相等(对于内容集)

我有四个字符串,如下所示.虽然它们具有不同的字符顺序和逗号后的不同间距 - 它们被认为具有same business value.

  1. 如何检查所有字符串是否相同(根据上面解释的业务场景)?我有以下代码,但在逗号后的空格情况下失败.
  2. 有什么比这更好的方法(为此目的)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)

c# linq

1
推荐指数
1
解决办法
3096
查看次数

如何比较 C# 中的两个列表?

我有两个列表要比较:

在此处输入图片说明

我想比较列表 A 和列表 B,如果列表 A 中存在列表 B 中的任何日期,则返回 true。

例如,如果 14-01-2020(在列表 B 中)出现在列表 A(肯定存在)中,那么它应该返回 true。

怎么做?

请注意:列表 A中的数据包含整个月的日期,而列表 B中仅包含几个日期。

c# list

1
推荐指数
1
解决办法
80
查看次数

比较对象列表

我有3个方法应该返回相同的数据.数据是一个列表,MyObjectModel我想检查三个列表是否包含相同的数据.我想到在json中序列化3个列表中的每个列表并计算字符串的长度是否都相同.

有更好的方法吗?

谢谢.

c#

0
推荐指数
1
解决办法
122
查看次数

标签 统计

c# ×7

collections ×2

comparison ×2

linq ×2

list ×2

.net ×1

asp.net ×1

assert ×1

compare ×1

equality ×1

nunit ×1

tuples ×1