简体   繁体   English

.net lambda表达式和out参数

[英].net lambda expression and out parameter

I have a WCF method which returns me an array of custom objects like "users", "roles", or something else, and it has page output. 我有一个WCF方法,它返回一个自定义对象数组,如“用户”,“角色”或其他东西,它有页面输出。 WCF method has out parameter, stored procedure select rows and return total records of all rows(not only selected), than i read return value in out parameter. WCF方法有out参数,存储过程选择行并返回所有行(不仅选中)的总记录,而不是i读出out参数中的返回值。 But there is one problem i call WCF-method in lambda expression: 但是我在lambda表达式中调用WCF方法有一个问题:

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

what better solution for my example? 我的例子有什么更好的解决方案

I haven't tried lambdas with out parameters but normally you just need to declare the variable beforehand: 我没有尝试过带参数的lambdas,但通常你只需要事先声明变量:

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

Edit : 编辑

Your best bet may by to wrap GetAllUsers with a separate class that can use the out param: 你可能最好的选择是将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;
    }

}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM