Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从列表<;字符串>;到字典<;字符串,字符串>;_C#_.net - Fatal编程技术网

C# 从列表<;字符串>;到字典<;字符串,字符串>;

C# 从列表<;字符串>;到字典<;字符串,字符串>;,c#,.net,C#,.net,我有一张单子 List<string> listOfAtt = new List<string>(); List listOfAtt=new List(); 其中listOfAtt[0]=“FirsName”,listOfAtt[1]=“Homer”等 如何创建此类词典 listOfAtt[“FirsName”]=“Homer”?假设listOfAtt.Count是偶数,并且偶数索引处的项目是唯一的,您可以在下面进行操作 Dictionary<string,st

我有一张单子

List<string> listOfAtt = new List<string>();
List listOfAtt=new List();
其中
listOfAtt[0]=“FirsName”
listOfAtt[1]=“Homer”

如何创建此类
词典


listOfAtt[“FirsName”]=“Homer”

假设
listOfAtt.Count
是偶数,并且偶数索引处的项目是唯一的,您可以在下面进行操作

Dictionary<string,string> dic = new Dictionary<string,string>();

for (int i = 0; i < listOfAtt.Count; i+=2) {
    dic.Add(listOfAtt[i], listOfAtt[i + 1]);
}
Dictionary dic=newdictionary();
对于(int i=0;i
假设
listOfAtt.Count
是偶数,并且偶数索引中的项目是唯一的,您可以在下面进行操作

Dictionary<string,string> dic = new Dictionary<string,string>();

for (int i = 0; i < listOfAtt.Count; i+=2) {
    dic.Add(listOfAtt[i], listOfAtt[i + 1]);
}
Dictionary dic=newdictionary();
对于(int i=0;i
最好的方法可能是使用for循环

Dictionary<string,string> dict = new Dictionary<string,string>();

for (int i = 0; i < listOfAtt.Count; i+=2){
    dict.Add(listOfAtt[i], listOfAtt[i+1]);
}
Dictionary dict=new Dictionary();
对于(int i=0;i
最好的方法可能是使用for循环

Dictionary<string,string> dict = new Dictionary<string,string>();

for (int i = 0; i < listOfAtt.Count; i+=2){
    dict.Add(listOfAtt[i], listOfAtt[i+1]);
}
Dictionary dict=new Dictionary();
对于(int i=0;i
假设键的唯一性,一种LINQ-y方法是:

Enumerable.Range(0, listOfAtt.Count / 2)
          .ToDictionary(x => listOfAtt[2 * x], x => listOfAtt[2 * x + 1]);
如果事情不是那么独特,您可以扩展此逻辑,按键分组并返回一个
字典,如:

Enumerable.Range(0, listOfAtt.Count / 2)
          .Select(i => new { Key = listOfAtt[2 * i], Value = listOfAtt[2*i+1] })
          .GroupBy(x => x.Key)
          .ToDictionary(x => x.Key, x => x.Select(X => X.Value).ToList());

假设键的唯一性,LINQ-y方法是:

Enumerable.Range(0, listOfAtt.Count / 2)
          .ToDictionary(x => listOfAtt[2 * x], x => listOfAtt[2 * x + 1]);
如果事情不是那么独特,您可以扩展此逻辑,按键分组并返回一个
字典,如:

Enumerable.Range(0, listOfAtt.Count / 2)
          .Select(i => new { Key = listOfAtt[2 * i], Value = listOfAtt[2*i+1] })
          .GroupBy(x => x.Key)
          .ToDictionary(x => x.Key, x => x.Select(X => X.Value).ToList());


所以前两个值是键值对,然后下两个,下两个,yadda yadda…?是的,这就是我想要做的。这里的用例是什么?列表中的值是否唯一?下面的答案都不考虑重复项,如果“FirsName”在列表中以偶数索引出现两次,则所有答案都将失败。应该是键的值是唯一的,因此这些答案我接受=),因此前两个值是键-值对,然后是下两个,下两个,yadda yadda…?是,这就是我想做的。这里的用例是什么?列表中的值是否唯一?下面的答案都不考虑重复,如果“FirsName”在列表中以偶数索引出现两次,则所有答案都将失败。应该是键的值是唯一的,因此这些答案我接受=)您输入了两次,而不是字符串;)复制并粘贴我打赌的问题;)@judgeja可能^^^我读得太快了,我没有在问题中看到它:PYou错了两次,用srting代替string;)复制并粘贴我打赌的问题;)@judgeja可能^^^我读得太快了,我没有在问题中看到它:PNice使用
可枚举范围
非常感谢!但是我可能会使用loop)很好地使用了
可枚举的.Range
非常感谢!但是我可能会使用loop)
newdictionary
应该是
newdictionary()
listOfAtt.count
应该是
listOfAtt.count
dict.add
应该是
dict.add
@Bacon Correct:)我主要使用vb.net,所以我通常不太担心大小写的敏感性或括号
新字典
应该是
新字典()
listOfAtt.count
应该是
listOfAtt.count
dict.add
应该是
dict.add
@Bacon Correct:)我主要使用vb.net,所以我通常不太担心大小写敏感或括号