使用 LINQ 表达式的文本表示
在我的项目中,我使用了大量 LINQ 表达式,并且允许用户通过在文本输入中键入 linq 查询来键入自己的查询
那么,是否可以从包含此表达式的文本创建 LINQ 表达式? 而且,是否有可能获得与 linq 查询等效的文本? (就像来自 FOO in bar select foo)
提前致谢
In my project, i use a lot of LINQ expressions and i would allow users to type their own queries by typing a linq query in a text input
So, is it possible to create a LINQ expression from a text containing this one ?
And, moreover, is it possible to get the text equivalent of a linq query ? (like from FOO in bar select foo)
Thank's by advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试NLinq:http://nlinq.codeplex.com
You can try NLinq: http://nlinq.codeplex.com
如果您要承担此任务,那么您本质上就是在执行编译器的工作(获取表达式的文本表示形式并从中创建表达式树。这是一项繁重的工作,可能不值得您花时间。我想到的是在 linq 查询中包含其他类型,然后动态编译它,这样做的明显缺点是,一旦程序集加载到内存中,就无法卸载它(除非开始使用 AppDomains)。 ),所以这基本上会变成内存泄漏,
因为您使用的是实体框架,所以最好的答案可能是使用 ESQL,http://innocraft.spaces.live.com/blog/cns!919A8CAC315ADF82!239.entry。这始终以文本格式使用,实体框架将 时运行查询。
为您解析它并在运行
If you were to take on this task, you would essentially be doing the job of the compiler (taking text representation of an expression and creating an expression tree out of it. This is a lot of work and probably not worth your time. The first thought that comes to mind is to have other's type in the linq query and then you compile it on the fly. The obvious downside to this is that once an assembly is loaded in memory, you can't unload it (unless you start using AppDomains), so this would basically turn into a memory leak.
Since you're using Entity Framework, the best answer is probably to use ESQL, http://innocraft.spaces.live.com/blog/cns!919A8CAC315ADF82!239.entry. This is always worked with in text format and Entity Framework will parse it out for you and run the query at runtime.
HTH
您是否可以为您的用户提供一份 LinqPad(免费),让他们可以创建自己的查询,并且还可以看到等效的 SQL 文本?
LinqPad 可以在此处找到
Is it possible you could give your users a copy of LinqPad (which is free), and they can create their own queries, and also see the SQL text equivalent?
LinqPad can be found here
您还可以考虑动态 LINQ。它仍然与 VS2010 捆绑在一起(位于 Samples 文件夹中),尽管它没有很好的文档记录。这里有一个简短的介绍:
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
You could also consider Dynamic LINQ. It's still bundled with VS2010 (somwehere in the Samples folder) although it's not very well documented. There's a brief intro here:
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx