c# - List to Dictionary with incremental keys in one line LINQ statement -
i have list:
var items = new list<string>() { "hello", "i value", "bye" };
i want convert dictionary following structure:
var dic = new dictionary<int, string>() { { 1, "hello" }, { 2, "i value" }, { 3, "bye" } };
as can see, dictionary keys incremental values, should reflect positions of each element in list.
i looking one-line linq statement. this:
var dic = items.todictionary(i => **specify incremental key or element index**, => i);
you can using overload of enumerable.select
passes index of element:
var dic = items.select((val, index) => new { index = index, value = val}) .todictionary(i => i.index, => i.value);
Comments
Post a Comment