检查两个列表的修改

发布于 2024-12-04 16:18:53 字数 463 浏览 1 评论 0原文

我有两个两种不同类型的列表,它们具有以下共同属性。

Id -->用于标识对应的对象

一堆其他Properties

ModificationDate

需要根据修改日期来比较这两个列表。如果修改了日期不同(第一个列表 ModificationDate 大于第二个列表的 ModificationDate 然后,如果该项目从第一个列表复制到第二个列表,则复制所有属性。

请让我知道最好的方法编辑

:第二。列表可能包含也可能不包含第一个列表的所有元素,反之亦然。我的第一个列表始终是源列表,因此如果某个项目存在于列表 1 中但不存在于列表 2 中,我们需要将其添加到列表 2 中。项目存在于列表 2 中但不在列表 1 中,然后将其从列表 2 中删除。

I have got two lists of two different type which has the following common properties.

Id -->used to identify corresponding objects

Bunch of other Properties

ModificationDate

Need to compare these two lists based on Modification date.If the modified date is different (first list ModificationDate greater than second list's ModificationDate then, copy all the properties if that item from first list to second.

Please let me know the best way to do this.

EDITED:Second list may or maynot contain all elements of the first and vice versa.My first list is always the source list. so if an item is present in list 1 and not present in list 2 we need to add it in list 2. also if an item present in list 2 but in not in list 1 then remove it from list2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦途 2024-12-11 16:18:53

查找添加/删除的项目

var list1 = new List<MyType>();
var list2 = new List<MyType>();

// These two assume MyType : IEquatable<MyType>
var added = list1.Except(list2);
var deleted = list2.Except(list1);

// Now add "added" to list2, remove "deleted" from list2

如果 MyType 未实现 IEquatable,或者该实现不仅仅基于比较 id,您将需要创建一个 IEqualityComparer< ;MyType>

class MyTypeIdComparer : IEqualityComparer<MyType>
{
    public bool Equals(MyType x, MyType y)
    {
        return x.Id.CompareTo(y.Id);
    }

    public int GetHashCode(MyType obj)
    {
        return obj.Id.GetHashCode();
    }
}

这将允许您执行以下操作:

// This does not assume so much for MyType
var comparer = new MyTypeIdComparer();
var added = list1.Except(list2, comparer);
var deleted = list2.Except(list1, comparer);

查找修改的项目

var modified = list1.Concat(list2)
                    .GroupBy(item => item.Id)
                    .Where(g => g.Select(item => item.ModificationDate)
                                 .Distinct().Count() != 1);

// To propagate the modifications:
foreach(var grp in modified) {
    var items = grp.OrderBy(item => item.ModificationDate);
    var target = items.First(); // earliest modification date = old
    var source = grp.Last();    // latest modification date = new

    // And now copy properties from source to target
}

Finding added/deleted items

var list1 = new List<MyType>();
var list2 = new List<MyType>();

// These two assume MyType : IEquatable<MyType>
var added = list1.Except(list2);
var deleted = list2.Except(list1);

// Now add "added" to list2, remove "deleted" from list2

If MyType does not implement IEquatable<MyType>, or the implementation is not based solely on comparing ids, you will need to create an IEqualityComparer<MyType>:

class MyTypeIdComparer : IEqualityComparer<MyType>
{
    public bool Equals(MyType x, MyType y)
    {
        return x.Id.CompareTo(y.Id);
    }

    public int GetHashCode(MyType obj)
    {
        return obj.Id.GetHashCode();
    }
}

Which will allow you to do:

// This does not assume so much for MyType
var comparer = new MyTypeIdComparer();
var added = list1.Except(list2, comparer);
var deleted = list2.Except(list1, comparer);

Finding modified items

var modified = list1.Concat(list2)
                    .GroupBy(item => item.Id)
                    .Where(g => g.Select(item => item.ModificationDate)
                                 .Distinct().Count() != 1);

// To propagate the modifications:
foreach(var grp in modified) {
    var items = grp.OrderBy(item => item.ModificationDate);
    var target = items.First(); // earliest modification date = old
    var source = grp.Last();    // latest modification date = new

    // And now copy properties from source to target
}
北恋 2024-12-11 16:18:53

这可能会有所帮助。 Linq 库有很多不错的函数,例如 except、Intersection。

http://msdn.microsoft.com/en-us/library/bb397894.aspx

This might be able to help. The Linq library has lots of decent functions, such as Except, Intersection.

http://msdn.microsoft.com/en-us/library/bb397894.aspx

吾性傲以野 2024-12-11 16:18:53

提供的链接有助于比较两个不同类型的列表

比较集合在.Net中

The provided link was helpful in comparing two lists of different types

Comparing Collections in .Net

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文