Wątek przeniesiony 2018-08-17 10:30 z C# i .NET przez somekind.

IEquatable – problem z porównaniem list

0

Witam, mam kolejny problem przy robieniu testów. Chcę sprawdzić działanie automappera gdzie mam także porównywanie list

public bool Equals(Product other)
        {
            bool equals =
                int.Equals(this.OriginalProductID, other.OriginalProductID) &&
                string.Equals(this.Barcode, other.Barcode, StringComparison.InvariantCultureIgnoreCase) &&
                string.Equals(this.BaseMeasureUnit, other.BaseMeasureUnit, StringComparison.InvariantCultureIgnoreCase) &&
                string.Equals(this.Description, other.Description, StringComparison.InvariantCultureIgnoreCase) &&
                string.Equals(this.SupplierSymbol, other.SupplierSymbol, StringComparison.InvariantCultureIgnoreCase) &&
                decimal.Equals(this.Vat, other.Vat) &&
                decimal.Equals(this.Quantity, other.Quantity) &&
                string.Equals(this.ImageUrl, other.ImageUrl, StringComparison.InvariantCultureIgnoreCase) &&
               **List<ProductPrice>.Equals(this.ProductPrices, other.ProductPrices) &&**
                **List<ProductLanguage>.Equals(this.ProductLanguages, other.ProductLanguages);**

            return equals;
        }

Problem jest bo te listy mają zerową długość a i tak sprawdzenie nie idzie pomyślnie:

immadiateWindow.PNG

Jest jakiś patent na sprawdzenie tych list? Potem będę musiał uporać się ze słownikami i też pewnie będzie podobny problem

0

Equals najpewniej porównuje referencje. Może użyj Enumerable.SequenceEqual

class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void SequenceEqualEx1()
{
    Pet pet1 = new Pet { Name = "Turbo", Age = 2 };
    Pet pet2 = new Pet { Name = "Peanut", Age = 8 };

    // Create two lists of pets.
    List<Pet> pets1 = new List<Pet> { pet1, pet2 };
    List<Pet> pets2 = new List<Pet> { pet1, pet2 };

    bool equal = pets1.SequenceEqual(pets2);

    Console.WriteLine(
        "The lists {0} equal.",
        equal ? "are" : "are not");
}

/*
 This code produces the following output:

 The lists are equal.
*/

EDIT: Zwracam jeszcze uwagę na to zdanie:

The SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) method enumerates the two source sequences in parallel and compares corresponding elements by using the default equality comparer for TSource, Default. The default equality comparer, Default, is used to compare values of the types that implement the IEqualityComparer<T> generic interface. To compare a custom data type, you need to implement this interface and provide your own GetHashCode and Equals methods for the type.

0

Niestety nawet nie mam takiej metody do wyboru jak SequenceEqual, zrobiłem tak

this.ProductPrices.Equals(other.ProductPrices) &&
this.ProductLanguages.Equals(other.ProductLanguages)

Niestety dalej to samo, zwraca false

0

No bo to są klasy, więc domyślnie porównuje referencje, skoro nie przeciążyłeś Equals. Ewidentnie brak Ci podstaw, skoro nie wiesz jak działa porównywanie obiektów w języku programowania, i że zazwyczaj nie jest to porównywanie wartości.
SequenceEquals nie masz, bo nie dodałeś using System.Linq;
A poza tym, to do tego, co chcesz zrobić istnieją już gotowe biblioteki, np. NUnit.Should albo CompareNETObjects.

1 użytkowników online, w tym zalogowanych: 0, gości: 1