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

Adding a filter on a row which contains null data causes a null reference exception #105

Open
riccardorestagno opened this issue May 15, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@riccardorestagno
Copy link

riccardorestagno commented May 15, 2023

Version

2.8.4

Details

When adding a filter on a string which contains null data, GridifyQueryable throws a null reference exception. Here is an example of the crash:

public PersonQueryResult<PersonUIModel> RunGetPerson(string Id, PagingRequest pagingRequest)
    {
        IEnumerable<PersonUIModel> persons= _context.Person
           .Where(ct => ct.Id== Id)
           .AsEnumerable()
           .Select(ct => new PersonUIModel
           {
               Id = ct.Id,
               FirstName = ct.FirstName,
               LastName = ct.LastName,
           });

        QueryablePaging<PersonUIModel> results = persons.AsQueryable().ApplyPageFiltering(pagingRequest, false);

        return new PersonQueryResult<PersonUIModel>()
        {
            TotalCount = results.Count,
            Transactions = results.Query.ToList()
        };
    }
    public static QueryablePaging<T> ApplyPageFiltering<T>(this IQueryable<T> query, PagingRequest pagingRequest, bool ignorePaging)
    {
        GridifyMapper<T> gridifyMapper = new GridifyMapper<T>(config => config.IgnoreNotMappedFields = true).GenerateMappings() as GridifyMapper<T>;

        return query.GridifyQueryable(PagingRequestConverter.ToQueryFilter(pagingRequest, ignorePaging), gridifyMapper);
    }

image

Workaround: Add null coalescing operator in query to default to string.Empty

FirstName = ct.FirstName ?? string.Empty,

Steps to reproduce

  • Add row in database which contains null string value
  • Add paging request filter to filter on said value (Ex: GridifyQuery.Filter = "FirstName=*ABC)
  • Result: Object reference not set to instance of object
  • Expected: Value not returned as per filter
@riccardorestagno riccardorestagno added the bug Something isn't working label May 15, 2023
@alirezanet
Copy link
Owner

alirezanet commented May 16, 2023

Hi @riccardorestagno,

Can you please check this discussion, I think you can find another workaround to solve it for now.
This is the default behavior since your filter query gets converted directly to a linq expression, so if you don't add the null check you get the exception.

FYI I'm planning to add a new feature to the next versions for adding the null checks automatically.

@riccardorestagno
Copy link
Author

Thanks for the info! My workaround works as well but I'll consider adding null checks in the expressions directly instead of adding a null coalescing operator in all my query properties.

@alirezanet alirezanet added enhancement New feature or request and removed bug Something isn't working labels May 16, 2023
@alirezanet alirezanet added feature-request and removed enhancement New feature or request labels Sep 22, 2023
@alirezanet alirezanet added enhancement New feature or request and removed feature-request labels Jun 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants