I need to filter a strongly typed list of type StaffingPositionsDataContract, with another list of filter names and values. I have these two lists:
List<SerializedForm> deserializedObject = JsonConvert.DeserializeObject<List<SerializedForm>>(searchFilters).Where(x => !string.IsNullOrEmpty(x.value) && !string.Equals(x.value.ToUpper(), "ALL")).ToList();
List<StaffingPositionsDataContract> staffingPositionResponse = new StaffingPositionsDataContract().LoadMockData();
The deserializedObject has 2 properties. 1: "name", 2: "value". These properties need to be able to filter several different classes with different properties. I have a method that works if the StaffingPositionsDataContract currently being filtered is a string, but not int or decimal or float. Below is what I am using that works with string filters only.
private static List<T> _GetFilteredList<T, U>(IList<T> ListToFilter, string PropertyToFilterOn, List<U> FilterValues)
{
ParameterExpression p = Expression.Parameter(typeof(T), "x");
Func<T, U> select = Expression.Lambda<Func<T, U>>(
Expression.Property(p, PropertyToFilterOn), p).Compile();
return ListToFilter.Join(FilterValues, select, u => u, (t, u) => t).ToList();
}
Here is how that is getting called:
var filteredPositions = staffingPositionResponse;
deserializedObject.ForEach(delegate(SerializedForm filters)
{
filteredPositions = _GetFilteredList<StaffingPositionsDataContract, string>(staffingPositionResponse, filters.name, new List<string> { filters.value });
});
Anyone know how I can filter the staffingPositionResponse class with the deserializedObject objects?
Best Solution
Here's a console app that does something similar. You'll have to evaluate how to get the proper json serialization from your app's form. I'm simply forcing a Dictionary into a json string for testing purposes.