.net - Generating completely dynamic SQL with linq -
i've been using linq entity framework while , have been loving it.
however, in situation need query dynamic database table on sql server. not know table name, structure or column names @ compile time.
is there way of using linq syntax compile sql query? given table name , column names @ runtime, , need complex searches (e.g. cola = x , (cola = y or colb = z) or (colc in n..p)), order by's , potentially group bys
i have found looks promising, http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx, don't understand a) how dynamically choose arbitrary table b) how prevent injection. author says there more in "part 2" - can't find links , blog 2008.
i imagining syntax like
public void selectpizzascheaperthan(int price) { var table = con.selectdb(tablename); var result = table.where(x => x.field<string>("food") = "pizza"); result = result.where(x => x.field<int>("price") < price); result = result.select(x => new {name = field<string>("name"), price = field<int>("price")}).tolist(); }
(obviously, hard coded strings arbitrary variables defined @ run time)
is there already? in particular, microsoft?
thanks reading
parameters injection safe.
sqlcommand.parameters property
use switch (if elseif) sqldbtype can build them dynamically.
Comments
Post a Comment