我有以下功能
public string StoredProcedure(string StoredProcedureName,List<List<string>> StoredProcedureParameter,string ReturnValue = null)
{
string functionReturnValue;
cmd = new SqlCommand(StoredProcedureName, conniction(true));
cmd.CommandType = CommandType.StoredProcedure;
for (int i = 0; i <= StoredProcedureParameter.Count; i++)
{
cmd.Parameters.AddWithValue(StoredProcedureParameter[0][i], StoredProcedureParameter[1][i]);
}
int exec_cmd = (int)cmd.ExecuteScalar();
if (ReturnValue != null)
{
SqlParameter prm = new SqlParameter(ReturnValue, SqlDbType.Int);
cmd.Parameters.Add(prm).Direction = ParameterDirection.ReturnValue;
functionReturnValue = prm.ToString();
}
else
{
functionReturnValue = exec_cmd.ToString();
}
return functionReturnValue;
}
我需要使用委托和lambda表达式将2D列表参数传递给StoredProcedureParameter
答案 0 :(得分:1)
执行以下操作。
更改方法的签名以接受委托:
public string StoredProcedure(string StoredProcedureName, Func<List<List<string>>> StoredProcedureParameterFunc, string ReturnValue = null)
{
List<List<string>> StoredProcedureParameter = StoredProcedureParameterFunc();
... // your original code here
}
调用如下方法:
var result = StoredProcedure("SPName", GetLists, "returnID");
其中GetLists
是返回“2D”字符串列表的新方法,例如:
private List<List<string>> GetLists() {
var result = new List<List<string>>();
... // fill result here
return result;
}