将字符串数组传递给LINQ存储过程

时间:2012-05-01 16:19:36

标签: c# linq stored-procedures parameters

有人能告诉我如何处理将一串字符串传递给LINQ存储过程吗?

通过将现有的sproc拖到dbml页面上来创建storedProc。 storedProc采用3个(或任意数量)参数来确定GetStringParams()中的值。

错误:No overload for storedProc takes 1 argument

DataClassDataContext db = new DataClassDataContext();
//storedProc takes 3 params
gridview.DataSource = db.storedProc(GetStringParams());
gridview.DataBind();

private string[] GetStringParams()
{
    string[] params = new string[3];    
    //Perform some logic here to determine which params to pass
    if (somethingIsTrue)
        params = new string[] { "param1", "param2", "param3" };
    else if (somethingElseIsTrue)
        params = new string[] { "param1", "param2", "" };
    else
        params = new string[] { "", "", "" };
    return params;
}

先谢谢。我搜索了这个问题,但找不到我要找的确切内容。优选地,不必修改sproc。

1 个答案:

答案 0 :(得分:1)

DataClassDataContext db = new DataClassDataContext();
//storedProc takes 3 params
string[] param = GetStringParams();
gridview.DataSource = db.storedProc(param[0], param[1], param[2]);
gridview.DataBind();

private string[] GetStringParams()
{
    //Perform some logic here to determine which params to pass
    if (somethingIsTrue)
        return new string[] { "param1", "param2", "param3" };
    else if (somethingElseIsTrue)
        return new string[] { "param1", "param2", "" };
    else
        return new string[] { "", "", "" };
}

除非方法要求使用params数组,否则无法将数组传递给多参数方法。