How can I change value in dictionary if my second parameter is list of objects?
I have this model:
public class Account
{
public string AccountType { get; set; }
public string AccountName { get; set; }
public BigInteger AccountNumber { get; set; }
public decimal Balance { get; set; }
public decimal AvailableBalance { get; set; }
public string Currency { get; set; }
public decimal InterestRate { get; set; }
}
I've made a dictionary where key is string and value is List of objects type of Account:
Dictionary<string, List<Account>> dictionaries = new Dictionary<string, List<Account>>();
I've filed list of accounts with some custom data:
List<Account> NMoAccounts = new List<Account>(){
new Account {AccountName="Credit card 1", AccountNumber=5234567890, AccountType="Credit card", AvailableBalance=234.4m, Balance=432.64m, Currency="euro", InterestRate=1.5m},
new Account {AccountName="Credit card 3", AccountNumber=1357924680, AccountType="Credit card", AvailableBalance=24.06m, Balance=-32.123m, Currency="euro", InterestRate=1.5m},
new Account {AccountName="Current card 10", AccountNumber=4567890123, AccountType="Current card", AvailableBalance=1.8m, Balance=2.3m, Currency="euro", InterestRate=1.5m},
new Account {AccountName="Credit card 5", AccountNumber=857624621, AccountType="Credit card", AvailableBalance=31.4m, Balance=-132.123m, Currency="euro", InterestRate=1.5m}
};
dictionaries.Add("user", NMoAccounts);
I would like to do something like this:
Something like I'm simulating money transaction between bank accounts.
Can I do this operation with dictionary? If someone has some tutorial with similar subject I would be more than thankful.
It is quite straight forward:
// find list in dictionary
List<Account> acctList = dictionaries[keyString];
// search account in list
Acount account = acctList.FirstOrDefault(a => a.AccountName = acctName);
if(account == null) // some error handling here
// modify account
accout.Balance -= 5;
Because the dictionary stores references and also the list contains references, you can modify these objects directly.
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.