c# dictionary values to list
var items = myDictionary.Values.ToList();
//Use Linq if you want to flattern your lists
var items = myDictionary.SelectMany (d => d.Value).ToList();
c# dictionary values to list
var items = myDictionary.Values.ToList();
//Use Linq if you want to flattern your lists
var items = myDictionary.SelectMany (d => d.Value).ToList();
dictionary to list c#
Dictionary<string, string> dicNumber = new Dictionary<string, string>();
List<string> listNumber = new List<string>();
dicNumber.Add("1", "First");
dicNumber.Add("2", "Second");
dicNumber.Add("3", "Third");
listNumber = dicNumber.Select(kvp => kvp.Key).ToList();
// Or:
listNumber = dicNumber.Keys.ToList();
c# array to dictionary
You can use the overload of Select which includes the index:
var dictionary = array.Select((value, index) => new { value, index })
.ToDictionary(pair => pair.value, pair => pair.index);
Or use Enumerable.Range:
var dictionary = Enumerable.Range(0, array.Length).ToDictionary(x => array[x]);
Note that ToDictionary will throw an exception if you try to provide two equal keys. You should think carefully about the possibility of your array having two equal values in it, and what you want to happen in that situation.
I'd be tempted just to do it manually though:
var dictionary = new Dictionary<string, int>();
for (int i = 0; i < array.Length; i++)
{
dictionary[array[i]] = i;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us