C# – How to filter a strongly typed list with List variables

asp.netc++lambdalinq

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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Reflection;

namespace FilterLists
{
    public class Program
    {
        static void Main(string[] args)
        {
            // set up the bunk json
            var filters = new Dictionary<string, object>();
            filters.Add("PositionId", "12345");
            string json = JsonConvert.SerializeObject(filters);
            // what's it look like as a string?
            Console.WriteLine(json);

            // take the json string and stuff it to our method.
            var result = GetStaffingPosition(json);
            Console.WriteLine(result.Count);
        }

    public static List<StaffingPositionsDataContract> GetStaffingPosition(string searchFilters)
    {
        var filters = JsonConvert.DeserializeObject<Dictionary<string, object>>(searchFilters).ToList();
        var contracts = StaffingPositionsDataContract.LoadMockData();

        List<StaffingPositionsDataContract> result = new List<StaffingPositionsDataContract>();

        foreach (var filter in filters)
        {
            foreach (var contract in contracts)
            {
                PropertyInfo info = typeof(StaffingPositionsDataContract).GetProperty(filter.Key);
                var propType = info.PropertyType;

                if (info.GetValue(contract, null).Equals(Convert.ChangeType(filter.Value, propType)))
                {
                    result.Add(contract);
                }
            }
        }

        return result;
    }
}


[Serializable]
public class StaffingPositionsDataContract
{
    public int PositionId { get; set; }
    public string Series { get; set; }
    public string BFY { get; set; }
    public string BudgetStatus { get; set; }
    public string DutyStation { get; set; }
    public string OrgLocation { get; set; }
    public string BudgetingEntity { get; set; }
    public string FieldName { get; set; }

    public static List<StaffingPositionsDataContract> LoadMockData()
    {
        List<StaffingPositionsDataContract> staffingposition = new List<StaffingPositionsDataContract>()
        {
            new StaffingPositionsDataContract() {PositionId = 12345, Series="", BFY="FY2010", BudgetStatus="Actual", DutyStation="", OrgLocation="", BudgetingEntity=""},
            new StaffingPositionsDataContract() {PositionId = 67891, Series="", BFY="FY2011", BudgetStatus="Actual", DutyStation="", OrgLocation="", BudgetingEntity=""},
            new StaffingPositionsDataContract() {PositionId = 12345, Series="", BFY="FY2010", BudgetStatus="Projected", DutyStation="", OrgLocation="", BudgetingEntity=""},
            new StaffingPositionsDataContract() {PositionId = 67892, Series="", BFY="FY2011", BudgetStatus="Projected", DutyStation="", OrgLocation="", BudgetingEntity=""},
            new StaffingPositionsDataContract() {PositionId = 987654, Series="", BFY="FY2010", BudgetStatus="Projected", DutyStation="", OrgLocation="", BudgetingEntity=""}
        };
        return staffingposition;
    }
}
}