Linq2Sql - Storing complex Linq queries for future dynamic execution - is the source code possible?

I have a lot of fun with Linq2Sql. The expression Trees was great, and just the standard Linq2Sql syntax was very funny.

Now I got into the part of my application where I need to somehow store queries in a database that are common for different clients that use the same database and the same tables (well, look, but you know that I I mean). Basically, I cannot do anything with hard code, and I have to leave the text of the request text open so that someone can write a new request like where-clause.

So, if this description was harsh, let me clarify:

In a previous version of our application, we used direct SQL calls to db using raw SQL. Yes. it was fun, dirty, and it worked. We will have a database table filled with various criteria such as

(EventType = 6 and Total > 0) 

or subquery style

 (EventType = 7 AND Exists ( select * from events as e1 where events.EventType = e1.EventType and e1.objectNumber = 89) ) 

(sql injection any?)

In Linq2Sql, this is a bit more complicated. I can do all of these queries without problems in the CLR, but I am able to transmit dynamic data, where the Linq criteria are a little more complicated, especially if I want to execute an additional query (for example, the above example).

Some ideas that I had:

Get the raw expression and save it --- but I don’t know how to take the source text expression and return it back to the executable.

Write a language similar to SQl and parse it and create a Linq expression - wow, it can be a lot of fun

I am sure there is SomeIqueryable.Where("EventType = 6 and Total > 54") . I read that it is available in beta 1, but I don’t understand how you can do it now.

 var exp2 = context.POSDataEventView.Where("EmployeeNumber == @0", 8310); 

That would be the easiest way to deploy .. I think.

Keep serialized expressions - wow .. this will confuse the user trying to write a query --- hell, I'm not sure I can even type everything.

So, I'm looking for some ideas on how I can store a query in some clear text and then execute it against Linq2Sql objects in some way without calling ExecuteSQL. I want to use LinqObjects.

PS I am using pLinqo for this application if this helps. Its still linq2sql though.

Thanks in advance!

+4
source share
1 answer

Perhaps the Dynamic LINQ library (in the MSDN examples) will help?

In particular, use as: .Where ("Category = 2 And UnitPrice> 3"

This should work with any IQueryable<T> source including LINQ-to-Objects, simply by calling .AsQueryable() in the sequence (usually IEnumerable<T> ).

+3
source

All Articles