gustavnavar / Grid.Blazor

Grid component with CRUD for Blazor (client-side and server-side) and ASP.NET Core MVC
GNU Lesser General Public License v2.1
696 stars 134 forks source link

Bug fix/no data on isnull filter #383

Closed Zapolatero closed 1 year ago

Zapolatero commented 1 year ago

Content of the PR This pr is a proposed solution for the bug explained in issue #381.

Explanation of the fixed bug

There was a problem in the way IsNull filters for text columns were generated by the DefaultColumnFilter class.

For example, when applying an "IsNull" filter on a column named "shipRegion", the expression generated for the filter was as follows :

e.shipRegion != null && e.shipRegion.ToUpper() == "".ToUpper()

This expression only includes data whose value for the filtered column is an empty string, and not a null string. This is a problem as we are trying to filter data whose value for this column is null.

Changes applied

In order to fixed this problem, I had to edit DefaultColumnFilter and TextFilterType files to change the way IsNull filters on text column are generated.

Changes on TextFilterType

In this class I added the GetNullOrEmptyCheck(Expression paramExpr) method which returns an expression calling string.IsNullOrEmpty on the filtered column. When querying the database, EF will translate this expression to this sql condition:

WHERE ([o].[ShipRegion] IS NULL) OR ([o].[ShipRegion] LIKE N'')

Including both data whose value for the filtered column is a null string or an empty string.

I then called this method in GetFilterExpression's switch, instead of GetCaseInsensitiveСomparation.

switch (filterType)
    {
        case GridFilterType.Equals:
            binaryExpression = GetCaseInsensitiveСomparation(string.Empty, leftExpr, valueExpr, removeDiacritics);
            break;
        case GridFilterType.IsNull:
            binaryExpression = GetNullOrEmptyCheck(leftExpr);
            break;

Changes on DefaultColumnFilter

I then changed the way GetExpression was called in the case of an IsNull filter on a string column (line 124), so that it only returns the expression generated by the GetNullOrEmptyCheck method, and not an expression of the type :

e.shipRegion != null && (string.IsNullOrEmpty(e.shipRegion))

Which would not make sense.

gustavnavar commented 1 year ago

Thank you for fixing this bug