Data grid filter for enum


            <RadzenDataGrid Responsive="true" ShowCellDataAsTooltip="LocalAppSate.ShowCellDataAsTooltip" @ref="@grid" Density="LocalAppSate.Density" AllowColumnResize="LocalAppSate.AllowColumnResize" IsLoading=IsLoading Count=@response.Data.TotalCount LoadData=@LoadData AllowSorting=true Data="@response.Data.Items" AllowFiltering="true" AllowPaging="true" PageSize="@requestInput.MaxResultCount" PagerHorizontalAlign="HorizontalAlign.Center">
                <Columns>
                    <RadzenDataGridColumn Property="ShortName" Title="Short Name" />
                    <RadzenDataGridColumn Property="UnitType" Title="Unit Type" />
                </Columns>
            </RadzenDataGrid>

We have a unit type defined as:

  public enum UnitTypeEnum
  {
      Mass = 10,
      Length = 20
  }

When applying a filter in the UI, it generates a filter like this:

x => (x.UnitType == (UnitTypeEnum)10)

Our backend uses:

System.Dynamic.Linq.Core

However, this throws the following exception:

Type 'Core.Enums' not found
      Type 'Core.Enums' not found (at index 56)
         at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseAsEnumOrNestedClass(String id)

It works correctly when we pass the filter like this:

x => (x.UnitType == 10)

instead of:

x => (x.UnitType == (UnitTypeEnum)10)

how can we just generate like this
x => (x.UnitType == 10)

instead of:

x => (x.UnitType ==(UnitTypeEnum) 10)

We abandoned this library due to vulnerability issues and now our expressions are only valid C#. You can use the provided info in LoadDataArgs to construct desired string if you insist to continue to use this library.

something like this on LoadData?

 if (!string.IsNullOrWhiteSpace(args.Filter))
 {
     args.Filter = args.Filter.Replace("(Core.Enums.UnitTypeEnum)", string.Empty);
 }

also @enchev based on this

Team have developed in-house replacement of System.Linq.Dynamic.Core, if we can get this as separate package so that we can use in our backend also.

We don't plan to introduce a separate package. You have access for free to our entire source code and you can use it in the way you need.

1 Like