实施自定义LINQ-to-X提供程序 [英] Implementing a custom LINQ-to-X provider

查看:82
本文介绍了实施自定义LINQ-to-X提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索工具,该工具接受复杂的搜索字符串(实际上是JSON中的n级对象图)并返回一些结果.我想通过类似LINQ的机制向其他(内部)开发人员公开该功能.

I've got a search tool which takes a complex search string (actually, an n-level object graph in JSON) and returns some results. I want to expose the functionality to other (internal) developers through a LINQ-like mechanism.

假定每个结果由类Result

我可以创建类似于以下内容的方法

I can create methods look something like:

Function Search(Expression As Linq.Expression(Of System.Func(Of Result, Boolean))) As IEnumerable(Of Result)

我不清楚的是如何遍历Expression参数并提取实际条件,然后将其形成对象图以运行查询.

What I'm unclear of is how I can walk that Expression parameter and extract the actual criteria which I can then form into the object graph for running my query.

有人可以向我指出如何实现此目标的教程/示例吗?

Can someone point me at a tutorial/example of how this can be achieved?

对于一些背景知识,我设法深入研究了表达式以获取递归结构,并能够检查lambda表达式的.Body,但是当我想处理Left时,和例如类型ExpressionType.Or的Node的Right属性,我遇到了问题.调试器正在使用Friend-scoped类(BinaryExpressionProxy)来检查对我不可用的表达式,因此我显然是走错了路.

For a little background, I've managed to dig into the expression far enough to get the recursive structure and am able to examine the .Body of lambda expressions but when I get as far as wanting to handle the Left and Right properties of a Node with eg type ExpressionType.Or, I'm having issues. The debugger is using a Friend-scoped class (BinaryExpressionProxy) to examine the expression which isn't available to me so I'm clearly heading down the wrong route.

推荐答案

您可能想看一下 ExpressionVisitor 类.这提供了遍历表达式树的机制,您所拥有的全部将覆盖处理每种类型的节点的方法.

You may want to look at subclassing the ExpressionVisitor class. This provides the mechanism to walk the expression tree, all you have override the methods to process each type of node.

此外,请参阅相关的(可能重复的)SO问题:

Also, see the related (possibly duplicate) SO question: Where are some good tutorials on writing a custom LINQ Provider?

以下一些其他链接可能会对您有所帮助:

Here are some other links you may find helpful:

表达式树(MSDN文章)

表达式树基础(博客帖子)

这篇关于实施自定义LINQ-to-X提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆