4
Answers

How do I Query Dynamically

Glenn Sanchez

Glenn Sanchez

12y
13.4k
1
How can I query on a table given as parameter? I just want to check if a record exists.

Example:

 String query = "Select * from " + tableName + "t where t.ID='" + fieldvalue + "'";

Regards
Answers (4)
0
Gohil Jayendrasinh

Gohil Jayendrasinh

NA 6.1k 2.8m 12y
0
Ajay Patel

Ajay Patel

NA 142 141.5k 12y

 String query = "Select * from " + tableName + "t where t.ID='" + fieldvalue + "'";

IEnumerable<Customer> results = db.ExecuteQuery<Customer>(query);

Class is your entity Class for returned result store
0
Glenn Sanchez

Glenn Sanchez

NA 133 30.3k 12y
Hi Senthilkumar,

Sorry for not being clear here.  My exact concern is how/what type of class should I use for casting result for dynamic query with LINQ.  This is what I mean:

String query = "Select * from " + tableName + "t (nolock) where t.ID='" + fieldvalue + "'";
var res = context.ExecuteQuery<ClassName>(query).ToList();

What should I use as class.
0
Senthilkumar

Senthilkumar

NA 15.2k 2.4m 12y
Hi,

You can query like this.

Are you going to use the table is not constant? then

string query = "SELECT * FROM " + tableName + " where ID='" + fieldvalue + "'";

You know the table name 
string query = "SELECT * FROM  tableName  WHERE ID='" + fieldvalue + "'";

If your id column is integer column then 

string query = "SELECT * FROM " + tableName + " WHERE ID=" + fieldvalue;