views:

42

answers:

2

Im working on an source code with an sql query in a VAR type like

var query = select ... from ... where ... ;

is it possible to add an dynamic "where clause" like

string condition = "where x.x > x.y"; 

e.g. var query = select ... from ... + condition;

Iam sorry for my bad english

A: 

You are not clearly stating how your query looks like. Is it a result of a LINQ operation or simply a String?

The keyword var is only usable for design time. The compiler will substitute it with the correct datatype.

If you SQL query is a string, like

var query = "Select ... from ... where ..";

then

string condition = "where x.x > x.y";
query += condition;

is valid because both variables are strings. You can't combine a non string type with a string the way your code suggests.

I do now assume that you are using a LINQ syntax. It is possible to add such conditions to a linq query per code, I think the keywords linq query builder, expression tree and predicate should get you started.

I'd strongly suggest that you stop using the var keyword without exactly knowing what it does and where to use it.

citronas
Your suggestion to avoid the var keyword is very subjective. Some people rather enjoy type inference. :)
Kirk Woll
A: 

Dynamic Linq exists specifically to solve late-bound scenarios for LINQ:

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

Allows constructs such as:

NorthwindDataContext northwind = new NorthwindDataContext();

var query = northwind.Products
                        .Where("CategoryID = 3 AND UnitPrice > 3")
                        .OrderBy("SupplierID");
Kirk Woll