【发布时间】:2015-07-03 21:06:54
【问题描述】:
让我们假设以下场景:
我有两个班级:
public class Customer
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Employee
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
然后我可以拥有:
List<Customer> customers = new List<Customer>();
List<Employee> employees = new List<Employee>();
现在我可以这样了:
public static List<Employee> GetMajorEmployees(this List<Employee> employees)
{
return employees.Where(t=>t.Age >= 18).ToList();
}
但是,与其在代码中运行查询,不如让用户在 UI 中定义它并在服务器端运行它,如下所示:
public static List<T> RunCustomQuery<T>(this List<T> items, dynamic query)
{
return items.ExecuteQuery(query); // to be implemented
}
其中query是用户在UI中构建并保存在数据库中的实际表达式。
如何在列表上构建动态表达式,并将其作为文本或 json 或 xml 保存到数据库中?
在 UI 上,用户可以选择/构建查询。对于客户列表,查询可能类似于:
customers.Where(t=>t.FirstName == "Jhon");
我还希望能够自动从动态对象中提取属性,因为您可以看到这两个类具有相同的属性但也有一些不同的属性。
有人能指出正确的方向吗?
我需要的是像 TFS 这样的东西:
【问题讨论】:
标签: c# expression expression-trees