kendo-labs / dlinq-helpers

Add server paging, filtering and sorting via Dynamic Linq
71 stars 70 forks source link

Convert to enum automatically #24

Open luizfbicalho opened 8 years ago

luizfbicalho commented 8 years ago

If one field of the grid is an enum, if you filter you get an error,

I added this code to change the enums to their correct values, i could be done inside the library

private static void UpdateFilter(Filter filter) { if(filter==null) { return; } if (filter.Field != null) { var campo = filter.Field.Split('.'); var val = filter.Value; var type = typeof(T); foreach (var c in campo) { type = type.GetProperty(c).PropertyType; } if (type.IsEnum) { val = val ?? "0"; try { var enumVal = Enum.Parse(type, val.ToString()); filter.Value = enumVal; } catch { }

        }
    }
    if(filter.Filters ==null)
    {
        return;
    }
    foreach (var item in filter.Filters)
    {
        UpdateFilter<T>(item);
    }
}