Convert LINQ query to string, send to remote serve

2019-07-29 07:40发布

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?

3条回答
不美不萌又怎样
2楼-- · 2019-07-29 08:13

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.

查看更多
贼婆χ
3楼-- · 2019-07-29 08:16

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...

查看更多
我想做一个坏孩纸
4楼-- · 2019-07-29 08:17

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>.

查看更多
登录 后发表回答