Ef core value comparer

Ef hate my value comparer
6 Replies
~FallenParadise~
I has HashSet on entity and converting it to string and back. It works but when im using value comparer (to track changes) it say always that A == B
internal class HashSetValueComparer : ValueComparer<ISet<string>>
{
public HashSetValueComparer() : base(
(a, b) => Compare(a, b),
c => c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode())),
x => x.Select(x => $"{x}").ToHashSet())
{ }

private static bool Compare(ISet<string> a, ISet<string> b)
{
Console.WriteLine(a.Equals(b));
Console.WriteLine(a.SequenceEqual(b));
Console.WriteLine(a == b);
return a == b;
}
}
internal class HashSetValueComparer : ValueComparer<ISet<string>>
{
public HashSetValueComparer() : base(
(a, b) => Compare(a, b),
c => c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode())),
x => x.Select(x => $"{x}").ToHashSet())
{ }

private static bool Compare(ISet<string> a, ISet<string> b)
{
Console.WriteLine(a.Equals(b));
Console.WriteLine(a.SequenceEqual(b));
Console.WriteLine(a == b);
return a == b;
}
}
if i clone x then equals and == always false (as expected) but SequenceEqual never false laso on PropertyEntry current values always equals old ones
Stroniax
Stroniax2y ago
SequenceEqual should be true if the two collections contain the same elements, so that should always be true for a clone until one collection is modified.
~FallenParadise~
well. for some reason it true but sequence is diff i mean it diff by ref
Stroniax
Stroniax2y ago
What are you passing into it? Thought I just read that you were cloning and testing SequenceEqual, sorry if I misread that.
~FallenParadise~
also its strange bc property current values and original values is same they just == also i fixed this issue somehow idk how
Stroniax
Stroniax2y ago
var coll = new HashSet<string>();
coll.Add("a");
coll.Add("b");

Assert.Equal(coll, coll);
Assert.True(coll.SequenceEqual(coll));

var clone = coll.Clone(); // not sure if this actually exists
Assert.NotEqual(coll, clone);
Assert.True(coll.SequenceEqual(clone));
var coll = new HashSet<string>();
coll.Add("a");
coll.Add("b");

Assert.Equal(coll, coll);
Assert.True(coll.SequenceEqual(coll));

var clone = coll.Clone(); // not sure if this actually exists
Assert.NotEqual(coll, clone);
Assert.True(coll.SequenceEqual(clone));
All those assertions should pass.
Want results from more Discord servers?
Add your server
More Posts