将字符串解析为 LINQ 查询

发布于 2024-10-26 06:57:09 字数 530 浏览 6 评论 0原文

将 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 引用 IEnumerableIQueryable 在本地范围内。

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 技术交流群。

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

发布评论

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

评论(6

虫児飞 2024-11-02 06:57:09

从 .NET 4.6 开始,您可以使用 CSharpScript 来解析 Linq。假设您要解析的表达式位于字符串变量“query”中,则可以这样做:

string query = "from element in source where element.Property = ""param"" select element";
IEnumerable result = null;
try 
{
    var scriptOptions = ScriptOptions.Default.WithReferences(typeof(System.Linq.Enumerable).Assembly).WithImports("System.Linq");
    result = await CSharpScript.EvaluateAsync<IEnumerable>(
             query,
             scriptOptions,
             globals: global);
} catch (CompilationErrorException ex) {
//
}

不要忘记传递您想要处理的(数据)源,并使用全局变量来访问它们在脚本解析中。

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:

string query = "from element in source where element.Property = ""param"" select element";
IEnumerable result = null;
try 
{
    var scriptOptions = ScriptOptions.Default.WithReferences(typeof(System.Linq.Enumerable).Assembly).WithImports("System.Linq");
    result = await CSharpScript.EvaluateAsync<IEnumerable>(
             query,
             scriptOptions,
             globals: global);
} catch (CompilationErrorException ex) {
//
}

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.

叶落知秋 2024-11-02 06:57:09

它需要一些文本解析并大量使用 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.

月依秋水 2024-11-02 06:57:09

您将需要一个 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

金兰素衣 2024-11-02 06:57:09

这可能适合您:C# eval 等效项?

This might work for you: C# eval equivalent?

_畞蕅 2024-11-02 06:57:09

这可能对您有帮助,也可能没有帮助,但请查看 LINQ 动态查询库

This may or may not help you, but check out LINQ Dynamic Query Library.

甜点 2024-11-02 06:57:09

虽然这没有具体给出一个例子来回答您的问题,但我认为最佳实践通常是从字符串构建表达式树。

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.

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