I have two ObservableCollection<Model>, each model has 10 properties and there are approximately 30 objects within both collections in the beggining. They basicaly work like this: initialy there are saved the same objects in both OCs, where one is the original and the other one is where changes are happening. Basicaly I would need the first one just to see if changes have been made to compare the values. So far I have come up with

list1.SequenceEquals(list2);

but this only works if i add a new object, it does not recognize changes in the actual properties. Is there a fast way this could be done or I need to do foreach for every object and compare individual properties one by one? Because there may be more than 30 objects to compare values. Thanks.

有帮助吗?

解决方案

Is there a fast way this could be done or I need to do foreach for every object and compare individual properties one by one?

If by "fast" you mean "performant" then comparing property-by property is probably the fastest way. If by "fast" you mean "less code to write" then you could use reflection to loop through the properties and compare the values of each item.

Note that you'll probably spend more time researching, writing, and debugging the reflection algorithm that you would just hand-coding the property comparisons.

A simple way to use the built-in Linq methods would be do define an IEqualityComparer<Model> that defines equality of two Model objects:

class ModelEqualityComparer : IEqualityComparer<Model>
{

    public bool Equals(Model m1, Model m2)
    {

        if(m1 == null || 2. == null)
            return false;

        if (m1.Prop1 == m2.Prop1 
         && m1.Prop2 == m2.Prop2
         && m1.Prop3 == m2.Prop3
            ...
           )
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    public int GetHashCode(Model m)
    {
        int hCode = m.Prop1.GetHashCode();
        hCode = hCode * 23 + ^ m.Prop2.GetHashCode();
        hCode = hCode * 23 + ^ m.Prop32.GetHashCode();
        ...

        return hCode;
    }

}

其他提示

I think you can compare them defining a custom IEqualityComparer<T>, and using the overload of IEnumerable.SequenceEqualsthat supports a custom comparer: Enumerable.SequenceEqual<TSource> Method (IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)more info about it here: http://msdn.microsoft.com/it-it/library/bb342073(v=vs.110).aspx

I'll post here an usage example from that page in case it goes missing:

Here is how to define a IEqualityComparer<T>

public class Product
{
    public string Name { get; set; }
    public int Code { get; set; }
}

// Custom comparer for the Product class
class ProductComparer : IEqualityComparer<Product>
{
    // Products are equal if their names and product numbers are equal.
    public bool Equals(Product x, Product y)
    {

        //Check whether the compared objects reference the same data.
        if (Object.ReferenceEquals(x, y)) return true;

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

        //Check whether the products' properties are equal.
        return x.Code == y.Code && x.Name == y.Name;
    }

    // If Equals() returns true for a pair of objects 
    // then GetHashCode() must return the same value for these objects.

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

        //Get hash code for the Name field if it is not null.
        int hashProductName = product.Name == null ? 0 : product.Name.GetHashCode();

        //Get hash code for the Code field.
        int hashProductCode = product.Code.GetHashCode();

        //Calculate the hash code for the product.
        return hashProductName ^ hashProductCode;
    }

}

Here's how to use it:

    Product[] storeA = { new Product { Name = "apple", Code = 9 }, 
                           new Product { Name = "orange", Code = 4 } };

    Product[] storeB = { new Product { Name = "apple", Code = 9 }, 
                           new Product { Name = "orange", Code = 4 } };

    bool equalAB = storeA.SequenceEqual(storeB, new ProductComparer());

    Console.WriteLine("Equal? " + equalAB);

    /*
        This code produces the following output:

        Equal? True
    */
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top