在这里,我使用linq在数组中过滤我的结果并传递到列表中,并从该列表转换为字典,如下所示
//array is a multidimensional array with string data in it
var datumn = array;
var list = new List<string>();
var stringcounts = new Dictionary<int,List<string>>();
var listtemp = new List<string>();
//linq
var arrayresult = from string a in datumn where a != "FREE" select a;
//adding result from arrayresult to list
foreach (var listing in arrayresult)
{
list.Add(listing);
}
//using linq again i filter my list then add to dictionary
for (int count = 3; count > 0; count-- )
{
var duplicateItems = from x in list
group x by x into grouped
where grouped.Count() == count
select grouped.Key;
foreach (var replace in duplicateItems)
{
listtemp.Add(replace.ToString());
}
stringcounts.Add(count, lists);
//clearing the list to avoid duplicating data in my dictionary
listtemp.Clear();
}
for (int key = stringcounts.Count; key > 0; --key)
{
var holding = stringcounts[key];
foreach (var li in holding)
{
MessageBox.Show(li.ToString());
//just view what i have to check if the data is correct
}
}
Run Code Online (Sandbox Code Playgroud)
`
程序跳过列表上的迭代器和结尾可以帮助这个,我已经尝试了包括linq和其他集合(如散列表和映射)在内的所有内容,但没有任何作用,它不是控制台应用程序
这条线错了:
var dict = new Dictionary<int, List<string>>();
Run Code Online (Sandbox Code Playgroud)
除掉 ";".
结果:
靛蓝银紫紫色绿色粉红色红棕黄色
编辑:完整代码进行比较:
var dict = new Dictionary<int, List<string>>()
{
{1, new List<string>{"red", "brown", "yellow"}},
{2, new List<string>{"purple", "green", "pink"}},
{3, new List<string>{"indigo", "silver", "violet"}}
};
// now i want to get my values from the lists in the dictionary
for (int count = 3; count > 0; count--)
{
var l = dict[count];
foreach (var li in l)
{
li.Dump();
}
}
Run Code Online (Sandbox Code Playgroud)