Skip to content

Fix QuickGrid nullable property sorting by handling UnaryExpression in ToPropertyName #62602

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

Merged
merged 10 commits into from
Jul 10, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 8, 2025

The QuickGrid component was failing when attempting to sort by nullable properties (like DateTime?) with the error:

The supplied expression can't be represented as a property name for sorting. 
Only simple member expressions, such as @(x => x.SomeProperty), can be converted to property names.

This occurred because the ToPropertyName method in GridSort.cs only handled MemberExpression but not UnaryExpression. When nullable properties are used in expressions that undergo implicit conversions (such as DateTime? to object), C# generates a UnaryExpression wrapping the MemberExpression.

Example of the issue:

public class TestEntity
{
    [Display(Name = "Tidspunkt")]
    public DateTime? TimeStamp { get; set; }
}

// This would fail before the fix
<DisplayNameColumn Property="@(p => p.TimeStamp)"
                   Format="dd.MM yy"
                   Sortable="true"
                   InitialSortDirection="SortDirection.Descending"
                   IsDefaultSortColumn="true">

Solution:

Updated the ToPropertyName method to handle UnaryExpression by unwrapping it to get the underlying MemberExpression, following the same pattern used in ModelBindingHelper.GetPropertyName.

The fix adds a check for UnaryExpression with Convert or ConvertChecked node types and unwraps them before proceeding with the existing logic:

// Handle UnaryExpressions that can occur due to implicit conversions, such as nullable value types
if (expressionBody.NodeType == ExpressionType.Convert ||
    expressionBody.NodeType == ExpressionType.ConvertChecked)
{
    expressionBody = ((UnaryExpression)expressionBody).Operand;
}

This change maintains full backward compatibility while enabling sorting on nullable properties.

Fixes #62582.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

…n ToPropertyName

Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Blazor quickgrid error - The supplied expression can't be represented as a property name for sorting Fix QuickGrid nullable property sorting by handling UnaryExpression in ToPropertyName Jul 8, 2025
@Copilot Copilot AI requested a review from javiercn July 8, 2025 15:19
Copilot finished work on behalf of javiercn July 8, 2025 15:19
Copilot AI and others added 3 commits July 8, 2025 15:29
…pertyName method

Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
@javiercn
Copy link
Member

javiercn commented Jul 8, 2025

@copilot D:\a_work\1\s\src\Components\Components.slnf references a project not in AspNetCore.slnx: src\Components\QuickGrid\Microsoft.AspNetCore.Components.QuickGrid\test\Microsoft.AspNetCore.Components.QuickGrid.Tests.csproj

Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 8, 2025

@copilot D:\a_work\1\s\src\Components\Components.slnf references a project not in AspNetCore.slnx: src\Components\QuickGrid\Microsoft.AspNetCore.Components.QuickGrid\test\Microsoft.AspNetCore.Compone...

Fixed by adding the QuickGrid test project to the main AspNetCore.slnx solution file. The test project is now properly included in both Components.slnf and AspNetCore.slnx. Commit: ca4430e

Copilot finished work on behalf of javiercn July 8, 2025 20:45
@javiercn
Copy link
Member

javiercn commented Jul 8, 2025

@copilot
src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/test/GridSortTest.cs(34,37): error CS1061: (NETCORE_ENGINEERING_TELEMETRY=Build) 'GridSort<GridSortTest.TestEntity>' does not contain a definition for 'ToPropertyList' and no accessible extension method 'ToPropertyList' accepting a first argument of type 'GridSort<GridSortTest.TestEntity>' could be found (are you missing a using directive or an assembly reference?)

…ructure for QuickGrid tests

Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 8, 2025

@copilot
src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/test/GridSortTest.cs(34,37): error CS1061: (NETCORE_ENGINEERING_TELEMETRY=Build) 'GridSort<GridSortTest.TestEntity>' does ...

Fixed by adding InternalsVisibleTo to the QuickGrid project and correcting the test structure. The test project now compiles successfully and all 11 tests pass, including comprehensive tests for nullable property sorting (the main fix) and edge cases. Commit: e5bcb38

Copilot finished work on behalf of javiercn July 8, 2025 21:22
@javiercn javiercn marked this pull request as ready for review July 9, 2025 15:15
@javiercn javiercn requested a review from a team as a code owner July 9, 2025 15:15
@javiercn javiercn enabled auto-merge (squash) July 9, 2025 21:22
Copy link

@Arnab-Developer Arnab-Developer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some refactor related suggestions.

javiercn and others added 3 commits July 10, 2025 13:36
…Grid/test/Microsoft.AspNetCore.Components.QuickGrid.Tests.csproj

Co-authored-by: Arnab Roy Chowdhury <arnab.roy.chowdhury.developer@gmail.com>
…Grid/test/GridSortTest.cs

Co-authored-by: Arnab Roy Chowdhury <arnab.roy.chowdhury.developer@gmail.com>
…Grid/test/GridSortTest.cs

Co-authored-by: Arnab Roy Chowdhury <arnab.roy.chowdhury.developer@gmail.com>
@javiercn javiercn merged commit c0b0121 into main Jul 10, 2025
28 checks passed
@javiercn javiercn deleted the copilot/fix-62582 branch July 10, 2025 15:32
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview7 milestone Jul 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Blazor quickgrid error - The supplied expression can't be represented as a property name for sorting
4 participants