2009-06-04 50 views
3

我需要使用另一個過濾器名稱和值列表過濾強類型StaffingPositionsDataContract的列表。我有這兩個列表:如何使用列表<String>變量過濾強類型列表

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

(重新評論) - 你能舉一個你如何調用它的例子嗎?什麼是價值?它適用於整數,字符串和浮點數... – 2009-06-04 17:48:00

回答

3

這是一個控制檯應用程序,做了類似的事情。您必須評估如何從應用程序的表單中獲取正確的json序列化。我只是爲了測試目的將一個字典強制爲一個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; 
    } 
} 
} 
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()); 
} 
+0

我用這種方法得到同樣的問題。錯誤是「類型'System.Int32'的表達式不能用於方法'Boolean Contains(System.String)'''的類型'System.String'的參數 – Zacho 2009-06-04 15:57:04