A quick way to check 2 lists is the same C #

Suppose these two strongly typed lists are:

List 1: existingitems

Identifier, name, cat
1, ABC, C
2, BCD, D
3, NNN, F

List 2: New Items

Identifier, name, cat
9, ABC, C
15, BCD, D
12, NNN, F

Basically, I want to check that the Name and Cat values ​​are the same in both lists. If the two lists are identical in the two columns, return true, otherwise false.

I tried several options mostly around below, but it always seems to return true, even if there is a new line in the newbie list that I expect to return false.

newitems.Any(x1 => existingitems.All(x2 => (x1.Name== x2.Name) && (x1.Cat== x2.Cat)));
+4
source share
4

, , , .

var list1Subset = list1.Select(i => new {i.Name, i.Cat});
var list2Subset = list2.Select(i => new {i.Name, i.Cat});

bool equal = list1Subset.SequenceEqual(list2Subset);
+4

HashSet :

public class ItemComparer : IEqualityComparer<Item>
{
    public bool Equals(Item x, Item y)
    {
        return (x.Cat == y.Cat) && (x.Name == y.Name);
    }

    public int GetHashCode(Item obj)
    {
        return (obj.Cat.GetHashCode() * 17) + (obj.Name.GetHashCode() * 17);
    }
}

public bool AreEqual(IEnumerable<T> set1, IEnumerable<T> set2, 
    IEqualityComparer<T> equalityComparer)
{
    // Handle cheapest cases
    if (set1 == null && set2 == null)
    {
        return true;
    }
    else if (set1 == null && set2 != null
        || set1 != null && set2 == null)
    {
        return false;
    }
    else if (object.ReferenceEquals(set1, set2))
    {
        return true;
    }

    var hashSet1 = new HashSet<T>(set1, equalityComparer);
    var hashSet2 = new HashSet<T>(set2, equalityComparer);

    // More easy cases
    if (hashSet1.Count != hashSet2.Count)
    {
        return false;
    }

    if (set1.Any(i => !hashSet2.Contains(i))
        || set2.Any(i => !hashSet1.Contains(i)))
    {
        return false;
    }

    return true;
}
+1
var areEqual = !existingitems.Select(x => new { x.Name, x, Cat })
                       .Except(newItems.Select(x => new { x.Name, x, Cat }))
                       .Any() 
             && !newItems.Select(x => new { x.Name, x, Cat })
                       .Except(existingitems.Select(x => new { x.Name, x, Cat }))
                       .Any();

var areEqual
     = newItems.Select(x => new { x.Name, x, Cat })
               .OrderBy(x => x.Name)
               .ThenBy(x => x.Cat)
               .SequanceEquals(existingitems.Select(x => new { x.Name, x, Cat })
                                       .OrderBy(x => x.Name)
                                       .ThenBy(x => x.Cat));
0

We can first define a method to determine if two sequences are equal sets:

public static bool SetEquals<T>(this IEnumerable<T> first
    , IEnumerable<T> second
    , IEqualityComparer<T> comparer = null)
{
    comparer = comparer ?? EqualityComparer<T>.Default;
    return new HashSet<T>(second).SetEquals(first);
}

Then we can filter out the necessary fields and fulfill the established equality for this projection:

var areEqual = list1.Select(item => new{item.Name, item.Cat})
    .SetEquals(list2.Select(item => new{item.Name, item.Cat}));
-1
source

Source: https://habr.com/ru/post/1526420/


All Articles