Adding more than one value to a Key in a Dictionary with Tuple Values

Mr.Jones

So, I'm writing a program that handles taxes. I've got a Dictionary that is set up as following:

static Dictionary<Commodity, Tuple<DateTime, double>> _TaxRates = new Dictionary<Commodity, Tuple<DateTime, double>>();

Commodity is an Enum that handles different tax areas (Food, Alcohol, Transport etc.). Now, I want to add a new tax rate, and the datetime that the tax rate was added.

So for instance, the Tax rate for Alcohol might have been 0,25% at 17:00, but at 17:25 it was 0,50% instead.

_TaxRates [commodity].Add(new Tuple<DateTime, double>(DateTime.Now, rate));

This gives me an error.

_TaxRates[commodity] =  Tuple.Create(DateTime.Now, rate);

And this seems to just overwrite the old value, meaning Alcohol has only ever had one Tax rate. Any helps or tips greatly appreciated.

"Full code" below:

            static Dictionary<Commodity, Tuple<DateTime, double>> _TaxRates= new Dictionary<Commodity, Tuple<DateTime, double>>();
        public void SetCustomTaxRate(Commodity commodity, double rate)
        {
            _TaxRates[commodity].Add(new Tuple<DateTime, double>(DateTime.Now, rate));
Richard Deeming

A Tuple only stores a single set of values. You want a Dictionary<Commodity, List<Tuple<DateTime, double>> instead.

private static readonly Dictionary<Commodity, List<Tuple<DateTime, double>> _TaxRates = new();

public void SetCustomTaxRate(Commodity commodity, double rate)
{
    if (!_TaxRates.TryGetValue(commodity, out var list))
    {
        list = new List<Tuple<DateTime, double>>();
        _TaxRates[commodity] = list;
    }
    
    list.Add(new Tuple<DateTime, double>(DateTime.Now, rate));
}

NB: Depending on your target framework, you may want to use a ValueTuple instead.
Tuple types - C# reference | Microsoft Docs

NB2: The code is not thread-safe. You should either lock the field, or use a ConcurrentDictionary<Commodity, List<(DateTime, double)>> instead.

NB3: Since you're using DateTime.Now, you should watch out for daylight savings time issues. Consider using DateTime.UtcNow or switching to DateTimeOffset instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I access values from a key that stores more than one value in a dictionary in python?

Dictionaries, adding more than one value with .get

Adding more than one GET value in the href

find the count of matching pair value in a dictionary where each key has more than one value

How to print the Key for particular value in python dictionary if more values are present for one key?

Delete all the keys from the dictionary if the value matches for more than one key

Find and print key which having more than one value in python dictionary

Python: merge a value of key if it exists more than one times within a same dictionary

setdefault - add more than one value to the key

How to convert the dictionary with one key and a tuple as the key value into a DataFrame?

Replace array value with more than one values

Adding list or dictionary in a dictionary with multiple values for one key - Python

Adding values to a dictionary key

Adding a key with values to a dictionary

Trying to grow a nested dictionary by adding more key:value pairs

Vectorized way of checking dataframe values (as key, value tuple) against a dictionary?

How to get more than one smallest /largest values of a dictionary?

Setting a value in nested dictionary sets it in more than one place

Return list with more than one value stored in a dictionary using linq

Interactive Grid - adding filter for more than one value

Adding more than one value to a single td in DataTables?

adding more than one value in replace method or split method

Adding two values to one key in lists and add the list to dictionary

Extracting values from more than one key in a list of dicts

How to add more than one values to a key in qjsonobject

Adding multiple key value pairs to a python dictionary and then checking for duplicate values

Access key values of dictionary with tuple as key

Dict - If more than one value per key, extract the last element

How to check if there is a key in collection that has more than one value?