简体   繁体   中英

.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 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. But there is one problem i call WCF-method in lambda expression:

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:

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:

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;
    }

}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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