Xero - beautiful accounting software

Xero Developer Help Center

Xero Developer Community

Community > Wrapper libraries >

Xero.NetStandard.OAuth2 For Each

Started by John Sutton -   in Wrapper libraries

Hi,

I am upgrading to use the OAuth2 from C# Xero Client.

My original code uses api.Accounts.Find().ToList() to get a list of accounts, I then do a foreach loop on the list of accounts.

With the new api I am using, Accounts Ledgers = await api.GetAccountsAsync(_accessToken, _xeroTenantId.ToString()); to get a list of accounts.

My problem is that I cannot do a foreach on the returned Accounts the error is "foreach statement cannot operate on variables of type 'Accounts' because 'Accounts' does not contain a public instance or extension definition for 'GetEnumerator'"

Any help/advise would be much appreciated.

Thanks
John
Solved it myself.

If anyone else finds this it was quite simple originally I had:-

foreach (var account in Ledgers)

Changed it to:-

foreach (var account in Ledgers._Accounts)

note the _Accounts
 

John Sutton  

Hi,

I am using .net SDK OAuth2 client and trying to get only revenue and sales accounts from list of accounts. Can you please let me know how to use where clause to filter the chart of accounts. My code is
await AccountingApi..GetAccountsAsync(_accessToken, _xeroTenantId.ToString());

Any help will be much appreciated.

Thank you
Angela
 

angela jetti  

Hi Angela,

This is my code with a filter/where

var Ledgers = await api.GetAccountsAsync(_accessToken, _xeroTenantId.ToString(),null, filter);

Hope it helps.

John
 

John Sutton  

Sorry the forum won't let me post the filter. I'll try again.

 

John Sutton  

var filter = "Type == backslash"DIRECTCOSTSbackslash" OR Type == backslash"REVENUEbackslash"";
 

John Sutton  

Hope that helps.

Replace backslash
 

John Sutton  

Hi John,

Thanks for your response. I am using a static method to get revenue or sales accounts as I need them to pop up in dropdownlist.

Oops, it doesn't allow me to copy the code.
 

angela jetti  

protected static List<Account> fGetXeroAccounts()
{
var filter = "Type == backslash"REVENUEbackslash" OR Type == backslash"SALESbackslash"";

var response = Task.Run(async () => await AccountingApi.GetAccountsAsync(accesstoken,XeroTenantId,null,filter,null));
 

angela jetti  

And the response for the above code is Id=38, Status=WaitingForActivation,Method="{null}",Result="{Not yet computed}"

Can you please let me know what I am doing wrong here.

By the way I replaced backslash

Thank you.
Angela.
 

angela jetti  

Looks like you need to wait for the result try the same code as mine.

var response = await api.GetAccountsAsync(_accessToken, _xeroTenantId.ToString(),null, filter);

response will be your list of accounts.
 

John Sutton  

I made it to work finally. I had to change the method to async instead of static to get the list of revenue accounts.

Thanks for your help.

Angela
 

angela jetti