No, there is no value you can pass that will turn that =
into <>
. You have to use a different query for this, either a distinct piece of code or a query built in pieces dependent on the requirements.
Personally I'd use LINQ for simplicity:
var qry = context.products.AsQueryable();
if (!string.IsNullOrEmpty(session))
qry = qry.Where(p => p.CategoryID == session);
You can go on adding terms like this, then execute the query at the end to get the results.
You can get a similar flexibility by creating the query as a string and parameter collection:
var qstr = "SELECT * FROM Products";
var parms = new Dictionary<string, object>();
if (!string.IsNullOrEmpty(session))
{
qstr += " WHERE CategoryID = @category";
parms["category"] = session;
}
using (var cmd = connection.CreateCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = qstr;
foreach (var parm in parms)
cmd.Parameters.AddWithValue("@" + parm.Key, parm.Value);
using (var reader = cmd.ExecuteReader())
{
//....
}
}
If you need more than the one form then you can add some logic to build a list of conditions. Just be careful how you stitch them together.
Still prefer the shorter LINQ solution :P