Finding an account (and its values) by Name
Posted: Jun 09 2024
I am trying to find account values by the name of the account (or any other identifier... Name seems good.)
I have tried using Lambda functionality similar to this:
But I am getting a syntax error that "Operator '?' cannot be applied to operand of type 'Account'" in the third line.
Is there a way out of this error?
I tried the long hand alternative, where I iterate through the .Items. It works, but it looks a bit tortured. Is there a better way?
I have tried using Lambda functionality similar to this:
Code: Select all
string account_ID = "MY_DEMO_ACCT";
var accounts = this.TradeManager.TradingData.Accounts.Items;
string matchingName = accounts.FirstOrDefault(a => a.Name == account_ID )?.Name;
if (matchingName != null)
{
Console.WriteLine("Found matching name: " + matchingName);
}
else
{
Console.WriteLine("No matching name found");
}
Is there a way out of this error?
I tried the long hand alternative, where I iterate through the .Items. It works, but it looks a bit tortured. Is there a better way?
Code: Select all
string account_ID = "MY_DEMO_ACCT";
var accounts = this.TradeManager.TradingData.Accounts.Items;
Account this_account;
int accounts_len = accounts.Length;
int account_idx=-1;
for(int i = 0; i < accounts_len; i++)
{
Output.WriteLine("accounts[{0}].Name: {1}, account_ID {2}", i, accounts[i].Name, account_ID);
if (account_ID == accounts[i].Name)
{
account_idx = i;
this_account = accounts[account_idx];
}
else {
Output.WriteLine("Could not find account");
Dispose();
}
}
// Finally I get account info here
Output.WriteLine(accounts[account_idx].Name);