I have been doing the following to pass parameters with my sql query:
var retval = context.MyObject.SqlQuery(sql.ToString(),
new SqlParameter("@ProductId", productId),
new SqlParameter("@CustomerId", customerId)).ToList();
I ran into a scenario where @CustomerId will not always be used in my sql string. Meaning I used an if statement to either add (Where p.CustomerId = @CustomerId) or keep it out.
I realized that I can't pass my parameters in directly to the SqlQuery function now. Instead I have to create an object list of parameters and then pass it in:
SqlParameter param1 = new SqlParameter("@ProductId", productId);
SqlParameter param2 = new SqlParameter("@CustomerId", customerId);
object[] parameters = new object[] { param1, param2 };
How can I make it so I can use an if statement to either add the CustomerId parameter to my parameters array or not?