Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to support multi-dimensional arrays #27618

Merged
1 commit merged into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,25 @@ private static void FormatParameterValue(StringBuilder builder, object? paramete
case IList list:
builder.Append("{ ");

for (var i = 0; i < list.Count; i++)
// Note: multi-dimensional arrays implement non-generic IList. They do not support indexing, but do support enumeration.
var isFirst = true;
foreach (var element in list.Cast<object>().Take(5))
{
if (i > 4)
if (isFirst)
{
builder.Append("...");
break;
isFirst = false;
}

FormatParameterValue(builder, list[i]);

if (i < list.Count - 1)
else
{
builder.Append(", ");
}

FormatParameterValue(builder, element);
}

if (list.Count > 5)
{
builder.Append(", ...");
}

builder.Append(" }");
Expand Down
5 changes: 4 additions & 1 deletion src/EFCore/ChangeTracking/ValueComparer`.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public ValueComparer(
var param1 = Expression.Parameter(type, "v1");
var param2 = Expression.Parameter(type, "v2");

// We exclude multi-dimensional arrays even though they're IStructuralEquatable because of
// https://github.com/dotnet/runtime/issues/66472
if (typeof(IStructuralEquatable).IsAssignableFrom(type))
{
return Expression.Lambda<Func<T?, T?, bool>>(
Expand Down Expand Up @@ -172,7 +174,8 @@ public ValueComparer(
protected static Expression<Func<T, T>> CreateDefaultSnapshotExpression(bool favorStructuralComparisons)
{
if (!favorStructuralComparisons
|| !typeof(T).IsArray)
|| !typeof(T).IsArray
|| typeof(T).GetArrayRank() != 1)
{
return v => v;
}
Expand Down
3 changes: 1 addition & 2 deletions test/EFCore.Tests/Storage/ValueComparerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void Throws_for_comparer_with_wrong_type()
[InlineData(typeof(float), (float)1, (float)2, null)]
[InlineData(typeof(double), (double)1, (double)2, null)]
[InlineData(typeof(JustAnEnum), JustAnEnum.A, JustAnEnum.B, null)]
[InlineData(typeof(int[]), new[] { 1, 2 }, new[] { 3, 4 }, null)]
public ValueComparer Default_comparer_works_for_normal_types(Type type, object value1, object value2, int? hashCode)
=> CompareTest(type, value1, value2, hashCode);

Expand Down Expand Up @@ -94,8 +95,6 @@ private static ValueComparer CompareTest(Type type, object value1, object value2
Assert.False(keyComparer.Equals(null, value2));
Assert.True(keyComparer.Equals(null, null));

Assert.Equal(hashCode ?? value1.GetHashCode(), keyComparer.GetHashCode(value1));

return comparer;
}

Expand Down