c# - context database.sqlQuery<type>() with dynamic parameter list -
i have problem sqlquery/exec. stored procedure returns list of entitys. need append parameterlist dynamicly.
using (mycontext db = new mycontext ()) { var queryparam = new dictionary<string, string>(); var arg = string.empty; var arglist = new list<objectparameter>(); var sql = string.empty; foreach(var line in param.split(',')) { var obj = line.split(':'); if (obj.length < 2) { continue; } if (obj[1].equals(string.empty)) { continue; } // arg = arg + "@" + obj[0] + "=" + "'" + obj[1] + "' "; arglist.add(new objectparameter(obj[0], obj[1])); } object[] prm = arglist.toarray(); // stored procedure ausführen und ergebniss erhalten. var list = db.database.sqlquery<dokument>("exec dbo.spdokumente", prm).tolist(); return appserverhelper.appresponse(list); }
on runtime application returns object type not match ne internal providers. dont have clue solve problem!
try using sqlparameter instead of objectparameter. follow sample code below:
public list<mycustomobject> selectload(datetime parameter1, datetime parameter2) { return execute("mystoredprocedure @parameter1, @parameter2", new object[]{new sqlparameter("@parameter1", parameter1), new sqlparameter("parameter2",parameter2)}); } private list<mycustomobject> execute(string query, object[] parameters) { var output = new list<mycustomobject>(); using (var dbcontext = new myentityframeworkdatacontext()) { output = dbcontext.database.sqlquery<mycustomobject>(query, parameters).tolist(); } return output; }
you can create parameters object array dynamically within first method.
Comments
Post a Comment