Using expression evaluator to execute dynamic LINQ strings on multiple Enumerables - DLR / NCalc / Flee?

爷,独闯天下 提交于 2019-12-12 04:07:20

问题


I have a generic object with properties:

Class MyObject
{
    public Dictionary<string, Object> properties = new Dictionary<string, Object>();
    public object this[string name] { get { return properties[name]: null; }
}
List<MyObject> People= new List<MyObject>();
People.Add(new MyObject(age = 50, home = "Dublin", name = "Bob", Income = "50"));
People.Add(new MyObject(age = 45, home = "London", name = "Tim", Income = "90"));

List<MyObject> Cities= new List<MyObject>();
Cities.Add(new MyObject(City = "Dublin", Country = "IE"));
Cities.Add(new MyObject(City = "London", Country = "UK"));

I would like to be able to query these Enumerables by using Linq, but these queries are dynamic and would be in strings, e.g.

People.
Where(a["age"] > 45 && Cities.Where(b["Country"] == "IE").Contains(a["home"]).
Sum(c["Income"])

Any recommendations? I'm currently using Ncalc with lots of switch statements that call Linq code...


回答1:


Have a look at Dynamic Linq



来源:https://stackoverflow.com/questions/3439637/using-expression-evaluator-to-execute-dynamic-linq-strings-on-multiple-enumerabl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!