开发者

Intercept the 'Expression' property of IQueryable Provider and gets its e-sql

开发者 https://www.devze.com 2023-04-07 00:57 出处:网络
Normally I can query an entity context using linq in the typical way, IQueryable<Product> query = from p in db.Products

Normally I can query an entity context using linq in the typical way,

IQueryable<Product> query = from p in db.Products
                            where p.ProductID > 1
                            select query;

Now say I wanted to get the expression of this IQueryable, serialize it, and pass it over the wire to be reconstructed on the other end,

Expression ex = query.Expression;

XElement xExpression = serializer.Serialize(ex); 

wcfClient.InvokeSomeServiceMethod(xExpression);

How would I reconstruct this on the other end? (the server end, to be specific)

I need to create a new instance of IQueryable and 'set' the expression used by the underlying provider by actually p开发者_C百科assing in an Expression instance, is this possible? There is no setter on IQueryable by default, of course. More specifically I just need an ObjectQuery so I can call ToTraceString() and get the entity sql on the other end. I cant seem to find any point where I can inject the Expression directly.

Thanks.


I would suggest you look at InterLINQ. It provides the ability to serialize an expression and transmit over WCF.

I'm using it with NHibernate to achieve a similar outcome to what you're describing and it seems to work well. (There's a bit to get your head around though, so as others have suggested, WCF Data Services / OData would be be quicker/easier).


Really, you have two options.

  1. Don't pass general queries over the wire. The server should have a GetProductsWithProductIdGreaterThan method that the client invokes.

  2. Look at WCF Data Services.

The third option, the one your requesting, is just hard.= Even if you're extremely fluent in LINQ, WCF, and Expression, it's still extremely difficult.

0

精彩评论

暂无评论...
验证码 换一张
取 消