-2

I'm currently working on a .NET Framework 4.7.2 application. Given is a Dictionary<string, object>. I need to write a method to transform the structure to a Dictionary<string, float>.

private List<KeyValuePair<int, Dictionary<string, float>>> CreateResult(List<Dictionary<string, object>> items)
{
    var result = new List<KeyValuePair<int, Dictionary<string, float>>>();

    for (int i = 0; i < items.Count; i++)
    {
       var item = items[i].ToDictionary<string, float>(v => v); // Error, wrong approach
       result.Add(new KeyValuePair<int, Dictionary<string, float>>(i, new Dictionary<string, float>(item)));
    }
    return result;
}

Unfortunately my method does not work, neither I don't really know if it's the right approach to transform all values of my dictionary to type float.

Do you know a good/save way to transform all values in my Dicitonary from type object to type float?

Thank you very much!

accordo777
  • 377
  • 3
  • 10

3 Answers3

2

You could convert the dictionaries and insert the index with a single (ugly) LINQ statement :

var result=items.Select((dct,i)=>new KeyValuePair<int, Dictionary<string, float>>(
                                           i,
                                           dct.ToDictionary(p=>p.Key,p=>(float)p.Value))
                       ).ToList();

A KeyValuePair doesn't offer any advantages except the property names. You could simplify the query by using value tuples:

var result=items.Select((dct,i)=>(i,dct.ToDictionary(p=>p.Key,p=>(float)p.Value)))
                 .ToList();

And name the tuple items in the return type :

private List<(int index , Dictionary<string, float> dict)> CreateResult(List<Dictionary<string, object>> items)
{
    var result=items.Select((dct,i)=>(i,dct.ToDictionary(p=>p.Key,p=>(float)p.Value)))
                    .ToList();
    return result;
}
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
1

When you know that the dictionary with object values contains only float values, you can do a typecast in ToDictionary:

private List<KeyValuePair<int, Dictionary<string, float>>> CreateResult(List<Dictionary<string, object>> items)
{
    var result = new List<KeyValuePair<int, Dictionary<string, float>>>();

    for (int i = 0; i < items.Count; i++)
    {
       var item = items[i].ToDictionary<string, float>(v => v.Key, v = (float)v.Value);
       result.Add(new KeyValuePair<int, Dictionary<string, float>>(i, item));
    }
    return result;
}

You can replace the typecast with any conversion that you deem necessary. The important thing is that the return type of the second lambda expression in ToDictionary is float.

Sefe
  • 13,731
  • 5
  • 42
  • 55
1

Try this

private List<KeyValuePair<int, Dictionary<string, float>>> CreateResult(List<Dictionary<string, object>> items)
    {
        var result = new List<KeyValuePair<int, Dictionary<string, float>>>();

        for (int i = 0; i < items.Count; i++)
        {
            var currentDic = new Dictionary<string, float>();
            var item = items[i];
            foreach (var itm in item)
            {
                currentDic.Add(itm.Key, (float)itm.Value);
            }

            result.Add(new KeyValuePair<int, Dictionary<string, float>>(i, currentDic));
        }
        return result;
    }
NaDeR Star
  • 647
  • 1
  • 6
  • 13