作为 C# 开发人员,我已进入第 8 天。
对于我正在处理的项目中的许多 DomainModel,我需要能够根据用户在审阅/搜索表单中提交的内容过滤表中的所有记录。
目前2美分的短途旅行是:
表单提交给 FooController/review。
Review 然后将所有键/值对从 Params['filter'] 抓取到一个字典中,并将其传递给一个名为 FooFinder.ByProperties 的助手类,它看起来非常类似于:
public IQueryable<WorkPlan> ByProperties( IDictionary<string, string> properties)
{
var result = ForSite(Convert.ToInt64(properties.DefaultVal("SiteId", "0")));
v);
if(properties.ContainsKeyAndIsNotNullOrEmpty("WorkPlan.Key"))
{
var tempVal = Convert.ToInt64(properties["WorkPlan.Key"]);
result = result.Where(r => r.Id == tempVal);
}
// Multiple of these conditional checks follows
return result;
}
我想尽可能减少重复代码并尝试类似
result = ByProperty(properties, "WorkPlan.key", typeof (Int64), r, v => r.id == v);
但由于很多原因,这显然行不通……我想要完成的目标仍然存在。我真的很想通过使用某种动态帮助程序/实用程序来简化代码并加快过滤过程。
我尝试的其他想法是使用反射和那个工作的孩子进行直接比较,但是如何检查诸如 CreatedDatime 属性之类的东西,我希望所有记录都大于“r => CreatedDatetime > CreatedFrom”。
如果这些都没有意义,请发表评论,我会尽力解决任何问题。