AutoFilterer icon indicating copy to clipboard operation
AutoFilterer copied to clipboard

Nested Queries don't work with CompareToAttribute

Open enisn opened this issue 4 years ago • 1 comments

Following situation doesn't work for Country.Name:

 public class CityFilterDto : BaseFilterDto
 {
     [CompareTo("Name", "Country.Name")]
    [StringFilterOptions(StringFilterOption.Contains)]
    public string Filter { get; set; }
}
  • Expected Expression is: .Where(x => x.Name.Contains("Turkey") || x.Country.Name.Contains("Turkey"))

  • Generated Expression is .Where(x => x.Name.Contains("Turkey").Where(x.Name.Contains("Turkey")))

enisn avatar Nov 20 '20 15:11 enisn

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; }
}
  1. 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; }
}
  1. 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.

enisn avatar Nov 20 '20 15:11 enisn