将字符串解析为 LINQ 查询
将 LINQ 字符串解析为查询的最佳实践是什么方法?
或者换句话说,哪种方法最适合转换:
string query = @"from element in source
where element.Property = ""param""
select element";
假设
IEnumerable<Element> = from element in source
where element.Property = "param"
select element;
source
引用 IEnumerable
或 IQueryable
在本地范围内。
What method would be considered best practice for parsing a LINQ string into a query?
Or in other words, what approach makes the most sense to convert:
string query = @"from element in source
where element.Property = ""param""
select element";
into
IEnumerable<Element> = from element in source
where element.Property = "param"
select element;
assuming that source
refers to an IEnumerable<Element>
or IQueryable<Element>
in the local scope.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从 .NET 4.6 开始,您可以使用 CSharpScript 来解析 Linq。假设您要解析的表达式位于字符串变量“query”中,则可以这样做:
不要忘记传递您想要处理的(数据)源,并使用全局变量来访问它们在脚本解析中。
Starting with .NET 4.6 you can use CSharpScript to parse Linq. Assuming the expression you want to parse is in string variable "query", this will do it:
Don't forget to pass your (Data)source you want to work on, with the global-variable(s) to have access to them in script parsing.
它需要一些文本解析并大量使用 System.Linq.Expressions< /a>.我在此处和此处。第二篇文章中的代码比第一篇文章有所更新,但仍有一些地方很粗糙。有时我会继续搞乱这个,并且有一个更干净的版本,如果您有兴趣,我一直想发布。我已经非常接近支持 ANSI SQL 89 的一个很好的子集了。
It requires some text parsing and heavy use of System.Linq.Expressions. I've done some toying with this here and here. The code in the second article is somewhat updated from the first but still rough in spots. I've continued to mess round with this on occasion and have a somewhat cleaner version that I've been meaning to post if you have any interest. I've got it pretty close to supporting a good subset of ANSI SQL 89.
您将需要一个 C# 语言解析器(至少 v3.5,可能是 v4.0,具体取决于您希望在 LINQ 中支持哪些 C# 语言功能)。您将获取这些解析器结果并使用访问者模式将其直接输入到表达式树中。我还不确定,但我敢打赌您还需要某种形式的类型分析来完全生成表达式节点。
我正在寻找和你一样的东西,但我并不是真的那么需要它,所以我没有努力搜索,也没有沿着这些思路编写任何代码。
我编写了一些东西,它接受用户字符串输入并使用
Microsoft.CSharp.CSharpCodeProvider
编译器提供程序类将其编译为动态程序集。如果您只想获取代码字符串并执行结果,那么这应该很适合您。以下是我编写的控制台工具 LinqFilter 的说明:
http://bittwiddlers.org/?p=141
这是源存储库。 LinqFilter/Program.cs 演示了如何使用编译器来编译 LINQ 表达式:
http ://bittwiddlers.org/viewsvn/trunk/public/LinqFilter/?root=WellDunne
You're going to need a C# language parser (at least v3.5, possibly v4.0, depending on what C# language features you wish to support in LINQ). You'll take those parser results and feed it directly into an Expression tree using a visitor pattern. I'm not sure yet but I'm willing to bet you'll also need some form of type analysis to fully generate the Expression nodes.
I'm looking for the same thing as you, but I don't really need it that badly so I haven't searched hard nor written any code along these lines.
I have written something that takes user string input and compiles it to a dynamic assembly using the
Microsoft.CSharp.CSharpCodeProvider
compiler provider class. If you just want to take strings of code and execute the result, this should suit you fine.Here's the description of the console tool I wrote, LinqFilter:
http://bittwiddlers.org/?p=141
Here's the source repository. LinqFilter/Program.cs demonstrates how to use the compiler to compile the LINQ expression:
http://bittwiddlers.org/viewsvn/trunk/public/LinqFilter/?root=WellDunne
这可能适合您:C# eval 等效项?
This might work for you: C# eval equivalent?
这可能对您有帮助,也可能没有帮助,但请查看 LINQ 动态查询库。
This may or may not help you, but check out LINQ Dynamic Query Library.
虽然这没有具体给出一个例子来回答您的问题,但我认为最佳实践通常是从字符串构建表达式树。
在this 问题我问如何使用字符串过滤 linq 查询,该字符串显示您构建表达式树的一部分。然而,这个概念可以扩展到构建代表字符串的整个表达式树。
请参阅 Microsoft 的这篇文章。
可能还有其他更好的帖子。另外,我认为像 RavenDB 这样的东西已经在其定义索引的代码库中做到了这一点。
While this doesn't specifically give an example to answer your question I would have thought the best practice would generally be to build an expression tree from the string.
In this question I asked how to filter a linq query with a string which shows you building a portion of an expression tree. This concept however can be extended to build an entire expression tree representing your string.
See this article from Microsoft.
There are probably other better posts out there as well. Additionally I think something like RavenDB does this already in its code base for defining indexes.