Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net lambda expression and out parameter

Tags:

c#

.net

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?

like image 849
Andrey Ashurko Avatar asked Nov 04 '22 07:11

Andrey Ashurko


1 Answers

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

}  
like image 61
D Stanley Avatar answered Nov 09 '22 13:11

D Stanley