【问题标题】:Compare two list for differences - DateTime & String比较两个列表的差异 - DateTime 到 String
【发布时间】:2022-02-08 18:59:16
【问题描述】:

对不起,这个问题没有代码...我希望没问题。

所以,我有一个包含两个属性 DateTime 和 String 的对象列表。

事实上,DateTime 就是日期(insertDate),例如 2022-02-04T00:00:00+01:00。 字符串当然是一些文本,很多文本:)

我必须将具有这两个属性的列表与另一个相同结构的列表进行比较。

为了更清楚,这些是API返回的数据,所以我必须比较这两个列表之间是否有任何区别。

【问题讨论】:

  • stackoverflow.com/questions/22173762/… 也许这会回答你的问题?
  • 不够你想比较什么??你有一个list1,你有一个list2,你想为list2新项目比较什么?同样的时间,同样的字符串......

标签: c# asp.net


【解决方案1】:

这样做(它会根据元素索引比较两个列表,所以 如果您的列表未排序,则必须对其进行排序,然后使用这些代码):

public class YourClass
{
    public DateTime DateTime { get; set; }
    public string String { get; set; }
}

public class Program
{
    static List<YourClass> list1 = new List<YourClass>
    {
        new YourClass { DateTime = DateTime.MinValue, String = "str1"},
        new YourClass { DateTime = DateTime.Now.AddDays(1), String = "str2"},
        new YourClass { DateTime = DateTime.Now.AddDays(2), String = "str3"}
    };

    static List<YourClass> list2 = new List<YourClass>
    {
        new YourClass { DateTime = DateTime.MinValue, String = "str1"},
        new YourClass { DateTime = DateTime.Now.AddDays(-1), String = "2str"},
        new YourClass { DateTime = DateTime.Now.AddDays(-2), String = "3str"}
    };

    static void Main(string[] args)
    {
        //a list of integer for storing different elements indexes
        var differentElementsIndexes = new List<int>();

        //check difference of these two list using for loop
        for (int i = 0; i < list1.Count; i++)
        {
            if (list1.ElementAt(i).DateTime != list2.ElementAt(i).DateTime || list1.ElementAt(i).String != list2.ElementAt(i).String)
                differentElementsIndexes.Add(i);
        }
    }

【讨论】:

  • 如果 List2 有比 list1 更多的元素(并且在 list1 的最后一个有序元素之后)你会错过这些差异
【解决方案2】:

您可以使用 CustomComparator:

给定两个列表的基类:

public class BaseClass 
{ 
    public DateTime date { get; set; }
    public string anyString { get; set; }
}

为其定义自定义比较器

public class BaseClassComparer : IEqualityComparer<BaseClass>
{
    public bool Equals(BaseClass item1, BaseClass item2)
    {
        //Check whether the compared objects reference the same data.
        if (Object.ReferenceEquals(item1, item2)) return true;

        //Check whether any of the compared objects is null.
        if (Object.ReferenceEquals(item1, null) || Object.ReferenceEquals(item2, null))
        return false;

        //Check whether the required fields' properties are equal.
        return (item1.date == item2.date) && (item1.anyString == item2.anyString);
    }

    public int GetHashCode(BaseClass item)
    {
        //Check whether the object is null
        if (Object.ReferenceEquals(item, null)) return 0;

        //Get hash code for the Date field if it is not null.
        int hashItemDate = item.date == null ? 0 : item.date.GetHashCode();

        //Get hash code for the string field.
        int hashItemString = item.anyString.GetHashCode();

        //Calculate the hash code for the item.
        return hashItemDate ^ hashItemString;
    }
}

那么你可以如下使用它:

        List<BaseClass> class1 = new List<BaseClass>();
        List<BaseClass> class2 = new List<BaseClass>();
        BaseClass item1 = new BaseClass() {date = DateTime.Now, anyString = "Hello"};
        class1.Add(item1);
        class2.Add(item1); //<-Equal till here
        //class1.Add(item1); //<-uncomment for false
        bool result = class1.SequenceEqual(class2, new BaseClassComparer());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多