.net lambda expression and out parameter

I have a WCF method that returns me an array of user objects such as "users", "roles" or something else, and has a page output. The WCF method has a parameter, stored procedures select rows and return full records of all rows (not just selected ones), than I read the return value in the out parameter. But there is one problem: I call the WCF method in a lambda expression:

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

what is the best solution for my example?

+5
source share
1 answer

I have not tried lambdas without parameters, but usually you just need to declare a variable in advance:

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

Edit

It’s best to conclude GetAllUserswith a separate class that can use a parameter 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;
    }

}  
+2
source

All Articles