简体   繁体   English

如何在运行时从文本构建LINQ查询?

[英]How to build a LINQ query from text at runtime?

I have a 我有一个

class A {
   public int X;
   public double Y;
   public string Z;
   // and more fields/properties ...
};

and a List<A> data and can build a linq query like eg List<A> data ,可以构建一个linq查询,例如

var q = from a in data where a.X > 20 select new {a.Y, a.Z};

Then dataGridView1.DataSource = q.ToList(); 然后dataGridView1.DataSource = q.ToList(); displays the selection in my DataGridView. 在我的DataGridView中显示选择。

Now the question, is it possible to build the query from a text the user has entered at runtime? 现在的问题是,是否可以从用户在运行时输入的文本构建查询? Like 喜欢

var q = QueryFromText("from a in data where a.X > 20 select new {a.Y, a.Z}");

The point being, that the user (having programming skills) can dynamically and freely select the displayed data. 关键是,用户(具有编程技能)可以动态且自由地选择显示的数据。

Dynamic Linq baby! 动态Linq宝贝!

re comment. 评论。

Yes, the example as written may not be possible using Dynamic Linq, but if you factor out the constants, eg 'from a in data' you are left with a 'where' and a 'select' which can be expressed with dynamic linq. 是的,使用Dynamic Linq可能无法编写示例,但如果您将常量分解出来,例如'from a in data',您将留下'where'和'select',它们可以用动态linq表示。

so two text boxes, maybe three if you include an orderby, could possibly satisfy your requirements. 所以两个文本框,如果包含orderby,可能是三个,可能满足您的要求。

Just a thought. 只是一个想法。

Jon has an interesting approach but i would be leery of compiling and executing unrestrained code. Jon有一个有趣的方法,但我会对编译和执行无限制代码持谨慎态度。

Well, you can use CSharpCodeProvider to compile code at execution time. 好吧,您可以使用CSharpCodeProvider在执行时编译代码。 Have a look at Snippy for an example of this. 看看Snippy就是一个例子。 In this case you'd need to compile the user code in a method which accepts a List<A> called data . 在这种情况下,你需要编译的用户代码中接受的方法List<A>称为data My experience is that it works, but it can be slightly fiddly to get right - particularly in terms of adding the appropriate references etc. 我的经验是它有效,但为了做到这一点可能会略显繁琐 - 特别是在添加适当的参考文献等方面。

Answering it pretty late; 很晚才回答它; though, it will help someone who visits this page. 但是,它会帮助访问此页面的人。

I had similar requirement and I solved it by dynamically compiling string as LINQ query, executing it over in-memory collection and collecting the result. 我有类似的要求,我通过动态编译字符串作为LINQ查询,通过内存中的集合执行它并收集结果来解决它。 Only catch is user input needs to be valid C# compile-able code else it returns an exception message instead of result. 只有catch是用户输入需要有效的C#可编译代码,否则它返回异常消息而不是结果。

Code is pretty long so here is the github link 代码很长,所以这里是github链接

Sample application on github shows multiple examples including projection. github上的示例应用程序显示了多个示例,包括投影。

Although there may be some ways to do this, LINQ simply isn't designed for this scenario. 虽然可能有一些方法可以做到这一点,但LINQ并不是为这种情况设计的。 Using CodeDOM (as Jon suggested) is probably the only way to get that easily done. 使用CodeDOM(如Jon建议的那样)可能是轻松完成任务的唯一方法。 If you trust the user and he/she has programming skills, you could perhaps just use old fashioned methods and let the user enter the query using SQL? 如果您信任用户并且他/她具有编程技能,您可能只是使用旧式方法并让用户使用SQL输入查询?

If you, on the other hand, choose to create some visual tool for constructing queries, you don't need to build them by composing strings and you can compose expression trees instead. 另一方面,如果您选择创建一些用于构造查询的可视化工具,则无需通过组合字符串来构建它们,而是可以组合表达式树。 For example using Linq Kit and AsExpandable . 例如,使用Linq KitAsExpandable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM