简体   繁体   English

将string []转换为List <KeyValuePair<int, string> &gt;

[英]Turn string[] into List<KeyValuePair<int, string>>

I have this strig[] : 我有这个strig []:

string[] SkippedAreasArray = new string[] {"A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q"};

I want to turn this into : 我想把它变成:

 List<KeyValuePair<int, string>>  SkippedAreasArray  = ???

I haven't been working with this List<KeyValuePair<int, string>> data structure. 我没有使用过这个List<KeyValuePair<int, string>>数据结构。 So I have to questions how would my string array looked like if I turn it to List<KeyValuePair<int, string>> (how to define it?). 所以我不得不质疑我的字符串数组如果将其转换为List<KeyValuePair<int, string>> (如何定义它?)。 And also - my logic is heavily depending on the indexes that are easily taken when I work with simple string[] . 而且 - 我的逻辑很大程度上取决于我使用简单的string[]时容易采用的索引。 The idea to use List<KeyValuePair<int, string>> is that I may need to use values like "A1" , "B1" , "B2" and I understand List<KeyValuePair<int, string>> is the better data structure to easily add those when I need but can I keep the correct indexing of the elements? 使用List<KeyValuePair<int, string>>的想法是我可能需要使用像"A1""B1""B2" ,我理解List<KeyValuePair<int, string>>是更好的数据结构在我需要的时候轻松添加那些但我可以保持元素的正确索引吗?

Is this what you are looking for: 这是你想要的:

string[] SkippedAreasArray = new string[] {"A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q"};
Dictionary<int,string> dictionary =
                 SkippedAreasArray.Select((r, i) => new { value = r, index = i })
                 .ToDictionary(t => t.index, t => t.value);

where key is the index and value is the alphabet. 其中key是索引, value是字母表。

OR 要么

List<KeyValuePair<int, string>> list =
    SkippedAreasArray.Select((r, index) => new KeyValuePair<int,string>(index, r))
    .ToList()

; ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 定义一个KeyValuePair结构,例如 <int,List<string> &gt; - Define a KeyValuePair struct like <int,List<string>> 排序列表 <KeyValuePair<int, string> &gt;大写和小写 - Sorting a List<KeyValuePair<int, string>> Uppercase and Lowercase gridview绑定下拉列表到列表 <keyvaluePair<int, string> &gt; - gridview bind dropdownlist to List<keyvaluePair<int, string>> c#排序列表 <KeyValuePair<int, string> &gt; - c# Sorting a List<KeyValuePair<int, string>> 如何正确使用列表上的LINQ查询 <KeyValuePair<string, string> 创建字典 <KeyValuePair<string, string> ,int&gt;? - How to properly use LINQ Query on List<KeyValuePair<string, string> to create Dictionary<KeyValuePair<string, string>, int>? 如何将int数组转换为List <KeyValuePair<int, string> &gt;? - How to convert int array to List<KeyValuePair<int, string>>? 如何访问列表 <KeyValuePair<string,int> &gt;来自MongoDB - How can I access List<KeyValuePair<string,int>> from MongoDB 保存清单 <KeyValuePair<int,string> 到使用EF 4.3的数据库 - Saving List<KeyValuePair<int,string> to a DB with EF 4.3 映射列表<KeyValuePair<string,int> &gt; 在 C# 中自定义对象 - Mapping List<KeyValuePair<string,int>> to custom object in C# 排序列表 <KeyValuePair<int,string> &gt;按C键 - To Sort a List <KeyValuePair<int,string>> by key in c sharp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM