enisn / AutoFilterer

AutoFilterer is a mini filtering framework library for dotnet. The main purpose of the library is to generate LINQ expressions for Entities over DTOs automatically. The first aim is to be compatible with Open API 3.0 Specifications
MIT License
458 stars 37 forks source link

Nested Queries don't work with CompareToAttribute #17

Open enisn opened 3 years ago

enisn commented 3 years ago

Following situation doesn't work for Country.Name:

 public class CityFilterDto : BaseFilterDto
 {
     [CompareTo("Name", "Country.Name")]
    [StringFilterOptions(StringFilterOption.Contains)]
    public string Filter { get; set; }
}
enisn commented 3 years ago

Temporary Solution

Temporary solution is creating a nested query.

  1. Create CountryFilterDto or use if it's already exist:

    public class CountryFilterDto : FilterBase
    {
    [StringFilterOptions(StringFilterOption.Contains)]
    public string Name { get; set; }
    }
  2. Place it into CityFilterDto as same name with Navigation property of Entity

    public class CityFilterDto : PaginationFilterBase
    {
    [CompareTo("Name")]
    [StringFilterOptions(StringFilterOption.Contains)]
    public string Filter { get; set; }
    
    public CountryFilterDto Country { get; set; }
    }
  3. Write a setter method for Filter and set Country.Name parameter at t he same time with Filter:

 public class CityFilterDto : PaginationFilterBase
 {
    private string filter;

    [CompareTo("Name")]
    [StringFilterOptions(StringFilterOption.Contains)]
    public string Filter { get => filter; set => SetFilter(value); }

    public CountryFilterDto Country { get; set; }

    private void SetFilter(string value)
    {
        filter = value; // keep backing field.

        if(Country == null)
            Country = new CountryFilterDto();

        Country.Name = value;

        this.CombineWith = CombineType.Or; // Make sure combines with || instead of  &&
    }
}

That's it! Nested query will work properly. This solution is temporary until solution of this issue.