[英]C# 2 string arrays to List<string, string>
Assuming the 2 string arrays are the same length and not empty how can I make a List of the contents? 假设2个字符串数组的长度相同且不为空,如何列出目录?
I had a Dictionary working, but now I need to be able to use duplicate keys so I am resorting to a List. 我有一个工作在词典中的字典,但是现在我需要能够使用重复的键,所以我要使用一个列表。
string[] files = svd.file.Split(",".ToCharArray());
string[] references = svd.references.Split(",".ToCharArray());
Dictionary<string, string> frDictionary = new Dictionary<string, string>();
frDictionary = files.Zip(rReferences, (s, i) => new { s, i })
.ToDictionary(item => item.s, item => item.i);
I could do it like: 我可以这样做:
List<string, string> jcList = new List<string, string>();
and then just have a double loop into the two arrays but I know a faster way must exist. 然后只有一个双循环进入两个数组,但是我知道必须存在一个更快的方法。
ILookup<string,string> myLookup =
files.Zip(rReferences, (s, i) => new { s, i })
.ToLookup(item => item.s, item => item.i);
will create a Dictionary
-like structure that allows multiple values per key. 将创建类似于Dictionary
的结构,每个键允许多个值。
So 所以
IEnumerable<string> foo = myLookup["somestring"];
A List containing elements with two strings each is simplest implemented with a 一个包含两个字符串的元素的列表最简单的实现是
List<T>
and 和
T == Tuple<string,string>
Then use a loop to build your list from the two arrays: 然后使用循环从两个数组构建列表:
string[] s1 =
{
"1", "2"
};
string[] s2 =
{
"a", "b"
};
var jcList = new List<Tuple<string,string>>();
for (int i = 0; i < s1.Length; i++)
{
jcList.Add(Tuple.Create(s1[i], s2[i]));
}
or using LINQ: 或使用LINQ:
var jcList = s1.Select((t, i) => Tuple.Create(t, s2[i])).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.