I need to construct a dynamic query based on user input.
I used to do it using SqlCommand with string concatenation.
Example:
public ActionResult Receipt_Details(string FromDate, string ToDate, int Branch, int User, int RecietType, int PayMethod)
{
if (FromDate == "")
FromDate = "1980-01-01";
if (ToDate == "")
ToDate = "2180-01-01";
string where = " where DmentDate>='"+FromDate+ "' and DmentDate<='"+ToDate +"'";
if (Branch != 0) // Branch = 0 means select all branches
where += " and BranchID = " + Branch;
if (User != 0) // User = 0 means select all users
where += " and UserID = " + User
if (PayMethod != 0)
where += " and PayMethodID = " + PayMethod
string constr = "data source=.;initial catalog=db;integrated security=True;";
using (SqlConnection con = new SqlConnection (constr))
{
con.Open();
using (SqlCommand com=new SqlCommand())
{
com.Connection = con;
com.command="Select * from Table "+ where;
}
}
}
Now I use Entity Framework. I want to use classes and linq or lambda expression
Thank you