1

I have a dictionary that will be populated from the results of a query. Due to this, I do not know what data values will be going into the dictionary when I initialize it (though obviously I do know what data types will be used). I'm new to C# - how can I set this up?

In pseudocode, the dictionary structure I want is:

{ "visa": [2.75, 3.33], "mastercard": [1.00, 4.32], ... } 

This is what I have so far, but its not compiling:

//initialize the dictionary but do not populate yet Dictionary<string, List<decimal>> cardtype_total_amount; //simulate getting the first card type from the db string cardtype = "visa"; //initialize the "visa" key if (!cardtype_total_amount.ContainsKey(cardtype)) cardtype_total_amount.Add(cardtype, new List<decimal>(){0, 0}); //simulate updating the values for "visa" from the db (this would happen lots of times for each card type): cardtype_total_amount[cardtype][0] += 0.5; cardtype_total_amount[cardtype][1] += 1.7; //add more keys for other cardtypes, and update their totals as per above... 
8
  • Do you know anything about the data? For instance, will it always consist of a string and two floats? Commented Nov 2, 2015 at 0:11
  • What do you mean "failing"? is it throwing an exception? Commented Nov 2, 2015 at 0:11
  • @KonradViltersten yes the data comes from SQL Server and will always be a string and two "money" type vars. Commented Nov 2, 2015 at 0:12
  • @YacoubMassad its not compliling Commented Nov 2, 2015 at 0:13
  • 1
    @mulllhausen I know precisely what you mean. Sometimes, one doesn't realize that the actual issue is elsewhere. I noticed by the way you asked that you (a) are less savvy on C# but (b) are technically skilled. My guess i that you're coming from JS or PHP direction. Glad to be helpful. +1 for (pretty) good formulation despite the uncertainty. Commented Nov 2, 2015 at 14:10

2 Answers 2

5

I think you're just missing an initalisation!

//initialize the dictionary but do not populate yet Dictionary<string, List<decimal>> cardtype_total_amount = new Dictionary<string, List<decimal>>(); 

[EDIT] Oh, and you need some m's on your decimals below, otherwise they are doubles:

cardtype_total_amount[cardtype][0] += 0.5m; 
Sign up to request clarification or add additional context in comments.

1 Comment

That's done it. I actually thought I was initializing the dictionary by declaring its type. I guess not.
3

Not sure if this is what you're after. How's this?

Dictionary<string, List<decimal> array = new Dictionary<string, List<decimal>>(); 

Then for every read in (consisting of a key and value) you could do the following.

var addition = new { Key = "visa", Value = 3.14 }; array[addition.Key].Add(addition.Value); 

Note that I'm not at the computer so I can have typoed a bit. Also, it depends a bit on how you're receiving the subsequent values. One at a time is assumed here. If you get a whole list of them, you can split that into dictionary itself.

List<Piece> bunchOfValues = ...; Dictionary<...> results = bunchOfValues.ToDictionary(key => key.NameOrType, value => bunchOfValues.Where(...).Select(...)); 

Finally, when you wish to sum it all, you can go LINQ again.

decimal sum = arrayOfValues.Sum(element => element); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.