开发者

How to dynamically assign database table and columns to linq?

开发者 https://www.devze.com 2023-03-20 08:32 出处:网络
I tried to call linq query from开发者_JS百科 sliverlight application to web service which is \'ADD.NET Entity Data Model\' with \'WCF Data Service\'.The linq below is working (e.g.using pre-defined ta

I tried to call linq query from开发者_JS百科 sliverlight application to web service which is 'ADD.NET Entity Data Model' with 'WCF Data Service'. The linq below is working (e.g.using pre-defined table & field names):

        var query = from o in context.ORDER
                    where o.NUMBER == 1
                    select o;

        ((DataServiceQuery<ORDER>)query).BeginExecute(OnQueryComplete, query);    

But I need dynamically assign different table and fields names to the linq query. Is there any way? Do I need to write a method in WCF to execute any sql command?

Thanks for any help.


You can use the Dynamic Linq samples to provide dynamic field names in where clauses - see: http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

Further, you can do this in a type-safe way using PredicateBuilder - http://www.albahari.com/nutshell/predicatebuilder.aspx

For more dynamic behavior - including dynamic table names - the only Linq option I can think of is to compile some code at runtime within your app using the CSharpCodeProvider (http://support.microsoft.com/kb/304655). However, obviously you need to be careful with security when offering this from a web service.

0

精彩评论

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