gpt4 book ai didi

c# - 如何使用 List 变量过滤强类型列表

转载 作者:行者123 更新时间:2023-11-30 15:12:39 26 4
gpt4 key购买 nike

我需要使用另一个过滤器名称和值列表来过滤类型为 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();

反序列化对象有两个属性。 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 类?

最佳答案

这是一个执行类似操作的控制台应用程序。您必须评估如何从应用程序的表单中获取正确的 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;
}
}
}

关于c# - 如何使用 List<String> 变量过滤强类型列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/951196/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com