.net lambda表达式和out参数

时间:2012-01-20 00:26:31

标签: c# .net

我有一个WCF方法,它返回一个自定义对象数组,如“users”,“roles”或其他东西,它有页面输出。 WCF方法有out参数,存储过程选择行并返回所有行(不仅选中)的总记录,而不是i读出out参数中的返回值。但是我在lambda表达式中调用WCF方法有一个问题:

var client = MySvcRef.MySvcClient();
var assistant = FormsAuthenticationAssistant();
var result = assistant.Execute<MySvcRef.UserClass[]>(
   () => client.GetAllUsers(out totalRecords, pageIndex, pageSize),
   client.InnerChannel);

我的例子有什么更好的解决方案?

1 个答案:

答案 0 :(得分:2)

我没有尝试过带参数的lambdas,但通常你只需要事先声明变量:

var client = MySvcRef.MySvcClient();
var assistant = FormsAuthenticationAssistant();
var totalRecords;
var result = assistant.Execute<MySvcRef.UserClass[]>(
  ()=>client.GetAllUsers(out totalRecords, pageIndex, pageSize), 
  client.InnerChannel);

修改

您最好的选择可以将GetAllUsers包含在可以使用out参数的单独类中:

Temp temp = new Temp();

var result = assistant.Execute<MySvcRef.UserClass[]>(()=>temp.GetAllUsers(client, pageIndex, pageSize),client.InnerChannel);
int totalRecords = temp.TotalRecords;

...

class Temp
{
    public int TotalRecords;
    public MySvcRef.UserClass[] GetAllUsers(MySvcClient client, int pageIndex, int pageSize)
    { 
        int totalRecords;
        var result = client.GetAllUsers(out totalRecords, pageIndex, pageSize);
        TotalRecords = totalRecords;
        return result;
    }

}