Reputation: 26311
Let's suppose I have the following dictionaries:
private Dictionary<int, string> dic1 = new Dictionary<int, string>()
{
{ 1, "a" },
{ 2, "b" },
{ 3, "c" }
}
private Dictionary<SomeEnum, bool> dic2 = new Dictionary<SomeEnum, bool>()
{
{ SomeEnum.First, true },
{ SomeEnum.Second, false },
{ SomeEnum.Third, false }
}
I want to convert these two dictionaries into a Dictionary<string, object>
For example:
dic1 = new Dictionary<string, object>()
{
{ "1", "a" },
{ "2", "b" },
{ "3", "c" }
}
dic2 = new Dictionary<string, object>()
{
{ "First", true },
{ "Second", false },
{ "Third", false }
}
As you can see, the string
key of these dictionaries is just the string
representation of the previous ones.
The method that is responsible for the conversion has the following signature:
public static object MapToValidType(Type type, object value)
{
//....
if(typeof(IDictionary).IsAssignableFrom(type))
{
//I have to return a Dictionary<string, object> here
return ??;
}
}
I have tried the following:
((IDictionary)value).Cast<object>().ToDictionary(i => ...);
But i
was casted to an object, so I cannot access the key or value items. For that I would need to cast it to the appropiate KeyValuePair<TKey, TValue>
, but I don't know TKey
or TValue
type.
Another solution is to do this:
IDictionary dic = (IDictionary)value;
IList<string> keys = dic.Keys.Cast<object>().Select(k => Convert.ToString(k)).ToList();
IList<object> values = dic.Values.Cast<object>().ToList();
Dictionary<string, object> newDic = new Dictionary<string, object>();
for(int i = 0; i < keys.Count; i++)
newDic.Add(keys[0], values[0]);
return newDic;
However, I'm not much of a fan of this approach and I am really looking for a simpler and friendlier one line LINQ statement.
Upvotes: 11
Views: 6961
Reputation: 2291
The trick is to cast your IDictionary to a generic type of DictionaryEntry. Then you can use ToDictionary() from System.Linq.
static Dictionary<string,object> ToDictionary(IDictionary dic)
{
return dic.Cast<DictionaryEntry> ().ToDictionary ((t) => t.Key.ToString (), (t) => t.Value);
}
Upvotes: 2
Reputation: 2399
public static IDictionary<string, object> Convert<TKey, TValue>(IDictionary<TKey, TValue> genDictionary)
{
return genDictionary.Select(kvp => new KeyValuePair<string, object>(kvp.Key.ToString(), (object)kvp.Value)).ToDictionary(x => x.Key, x => x.Value);
}
called like:
var dicIntInt = new Dictionary<int, string>{{123, "asdc"}, {456, "aa"} };
Dictionary<string, object> dicStrObj = Convert(dicIntInt);
https://dotnetfiddle.net/eY41MQ
Upvotes: 0
Reputation: 3805
static void Main(string[] args)
{
var blah = KeyToString(dic1);
// Verify that we converted correctly
foreach (var kvp in blah)
{
Console.WriteLine("{0} {1}, {2} {3}", kvp.Key.GetType(), kvp.Key, kvp.Value.GetType(), kvp.Value);
}
}
static Dictionary<string, TValue> KeyToString<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> dic1)
{
return dic1.ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value);
}
Upvotes: 1
Reputation: 787
You can try this, no LINQ though, i think you don't need:
Dictionary<string, object> ConvertToDictionary(System.Collections.IDictionary iDic) {
var dic = new Dictionary<string, object>();
var enumerator = iDic.GetEnumerator();
while (enumerator.MoveNext()) {
dic[enumerator.Key.ToString()] = enumerator.Value;
}
return dic;
}
Or a Linq one:
return iDic.Keys.Cast<object>().ToDictionary(k=> k.ToString(), v=> iDic[v]);
Upvotes: 7