使用表达式求值器在多个 Enumerable 上执行动态 LINQ 字符串 - DLR / NCalc / Flee?

发布于 2024-09-13 07:44:01 字数 967 浏览 1 评论 0原文

我有一个具有属性的通用对象:

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"));

我希望能够使用 Linq 查询这些枚举,但这些查询是动态并且是字符串形式,例如

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

有什么建议吗?我目前正在使用 Ncalc 以及许多调用 Linq 代码的 switch 语句...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文