Convert LINQ query to string, send to remote server to parse into an expression using Roslyn?

巧了我就是萌 提交于 2019-12-11 04:59:38

问题


I have a datasource such as IDataSource : IEnumerable<IDynamicObject>

One implementation is quering pure XML-data and another is quering an SQL database.

My datasources have no conrete types, and are only working against simple schema-types.

In some scenarios, these datasources are instanced in the same runtime-context and other they are accessed over the network.

I wasn't to be able to query my datasources with LINQ, and I'm planning on using dynamic objects to only extract and parse the member values that are part of the actual query.

This works fine when running in the same runtime-context, but when the datasource is behind a web service on a remote server, this fails because I can't use LINQ with a web service in any way I know of.

So I was looking for a method of formatting my LINQ queries as a string and send this to the server, then parse it back to an expression and execute it in the new context.

Two methods of parsing expression I could find was Dynamic LINQ and FLEE. I have only looked at them briefly but none of them seemed to be able to format an expression to a string as well as parse it?

I've read about the Roslyn-project and was thinking maybee it could be used for this problem? Anyone know more about that?

I've looked some at IQueryable<TData> as well but I've read a lot of negative things about it so maybe I should avoid it. Also it is strongly typed which doesn't fit my needs because I don't wan't any concrete types on the data server. I also don't see how this can be used over a web service, being strongly typed? Am I missing something?


回答1:


Roslyn will eventually give you the ability to take a LINQ expression in string form, parse it, and evaluate it. However, the current public CTP (the one released October 2011) does not support LINQ expressions.




回答2:


This sounds more like what you need is to be able to serialize expression trees, but not necessarily as a C# string.

The Expression Tree Serialization sample on MSDN does exactly that. It even includes a sample WCF service and cient that do exactly what you want, using IQueryable<T>.




回答3:


I think I've found my answer now:

WCF Data Services!

How could I have missed that! I though it was for Entity Framework only...



来源:https://stackoverflow.com/questions/9647983/convert-linq-query-to-string-send-to-remote-server-to-parse-into-an-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!