我需要过滤一个强类型的StaffingPositionsDataContract类型列表,以及另一个过滤器名称和值列表。我有这两个清单:
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();
deserializedObject有2个属性。 1:“名称”,2:“价值”。这些属性需要能够过滤具有不同属性的多个不同类。我有一个方法,如果当前正在过滤的StaffingPositionsDataContract是一个字符串,但不是int或decimal或float。以下是我正在使用的仅适用于字符串过滤器的内容。
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();
}
以下是如何调用它:
var filteredPositions = staffingPositionResponse;
deserializedObject.ForEach(delegate(SerializedForm filters)
{
filteredPositions = _GetFilteredList<StaffingPositionsDataContract, string>(staffingPositionResponse, filters.name, new List<string> { filters.value });
});
任何人都知道如何使用deserializedObject对象过滤staffingPositionResponse类?
答案 0 :(得分:3)
这是一个类似的控制台应用程序。您将不得不评估如何从您的应用程序表单中获取正确的json序列化。我只是将Dictionary强制转换为json字符串以进行测试。
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;
}
}
}
答案 1 :(得分:2)
请注意,如果在编译时无法知道属性名称,则应该只使用此方法;但以下似乎有效:
public static IEnumerable<T> WhereAny<T, U>(
this IEnumerable<T> ListToFilter, string PropertyToFilterOn, List<U> FilterValues)
{
ParameterExpression p = Expression.Parameter(typeof(T), "x");
var methodCall = Expression.Call(
Expression.Constant(FilterValues, typeof(List<U>)),
typeof(List<U>).GetMethod("Contains"),
Expression.PropertyOrField(p, PropertyToFilterOn)
);
return ListToFilter.Where(Expression.Lambda<Func<T,bool>>(methodCall, p).Compile());
}