In winform application, binding the Listbox with Dictionary via BindingSource property.
How do I get this BindingSource back to original Dictionary by type casting?
Eg:
Dictionary<string, string> objDic = getData();
OrderedDictionry ord = GetOrderedDict(objDic)
listBox.DataSource = new BindingSource(ord , null);
listBox.DisplayMember = "Value";
listBox.ValueMember = "Key";
Now, I want same Dictionary type value from listBox.DataSource for Linq query!!.
Eg:
var r = from t in (listBox.DataSource as Dictionary<string, string>).AsEnumaerable()
select t;
throws error?
How to type cast to dictionary ?
You’re trying to cast a BindingSource to a Dictionary. You need to cast the BindingSource’s DataSource.
I don’t think you can cast from
OrderedDictionarytoDictionary<>, but it would be easy to just reconstruct theDictionary<string, string>:If you want a LINQ version, you could do: