【问题标题】:Compare two list with out using linq for update History比较两个列表而不使用 linq 更新历史
【发布时间】:2020-12-01 19:39:00
【问题描述】:

嗨,我正在开发一个用 c# webform 编写的项目,如果有人更新了我应该插入到日志表中的数据,但我需要捕获正在更新的列或字段,它会要求更改日志历史。 所以我认为比较 2 列出更新前的 oldList 和更新后的列表 如果我有这门课

public myClass class{
public string col1{get; set;}
public string col2{get; set;}
public string col3{get; set;}
}

并像这样用数据列出

List<myClass > old = new List<myClass >();
List<myClass > new = new List<myClass >();

我怎样才能在不使用 Linq 的情况下编译这 2 并获取差异的列和新旧数据,因为该应用程序是现有的并且使用 .net 2.0 框架所以我相信 Linq 是暂无此功能。

由于某种原因,我不需要更新框架。

【问题讨论】:

  • 我给你for 循环和if 声明...... .Net Framework 2(2005 年投诉)......当你尝试这个时,什么不是为你工作?
  • 是的,我可以比较数据,我怎样才能确定哪一列有差异,对不起,我还是新手
  • 一个字符串,一个字符串列表?
  • if(old[i].col1 != new[i].col1) Console.WriteLine("Col1 is diffrent")
  • 是的,因为我有很多列

标签: c# asp.net .net webforms


【解决方案1】:

在你的类中实现 IComparable 接口

public class myclass : IComparable<myclass>
        {
            public string col1 { get; set; }
            public string col2 { get; set; }
            public string col3 { get; set; }

            public int CompareTo(myclass obj)
            {
                return this.col1.CompareTo(obj.col1);
            }
        }

然后对列表进行排序并遍历它

            oldlist.Sort();
            newlist.Sort();
            for (int i = 0; i < oldlist.Count; i++)
            {
                string oldcol1 = oldlist[i].col1;
                string newcol1 = newlist[i].col1;
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多