I have code that gets a query using a linq statement making it a DbQuery
and not ObjectQuery
and I was adding parameters through a foreach loop using that query. But DbQuery
has no parameter support. I know I could add them manually. But I have 36 different parameters in my SQL statement. So I need to find a way to add the parameters.
foreach (var parameter in query.Parameters)
{
parameters.Add(new System.Data.SqlClient.SqlParameter { ParameterName = parameter.Name, Value = parameter.Value });
}
entities.Database.ExecuteSqlCommand(sql, parameters.ToArray());
That is the code im using for the parameters.
I need a way to do that with DbQuery
You can compose a
DbQuery
by appending expressions to it. A pattern often used to do that is:I don't know if this would work in your situation. If not you can always resort to
ObjectQuery
. ADbContext
is a wrapper around anObjectContext
and you can get the latter by((IObjectContextAdapter)dbContext).ObjectContext
. That's a starting point to create ObjectQueries.I create an extension method to get the parameters
usage