0

在这里,我使用 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
    }

}

`
该程序跳过列表的迭代器并结束了一些人可以帮助解决这个问题,我已经尝试了所有东西,包括linq和其他集合,如哈希表和地图,但没有任何效果,它不是控制台应用程序

4

3 回答 3

7

这一行是错误的:

   var dict = new Dictionary<int, List<string>>();

去除 ”;”。

结果:

靛蓝 银色 紫罗兰色 紫色 绿色 粉色 红色 棕色 黄色

编辑:比较的完整代码:

   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();
    }
}
于 2013-06-07T11:25:38.430 回答
1
foreach (var item in dict)
        {
            var list = item.Value;
            foreach (var str in list)
            {
                MessageBox.Show(str);
            }
        }
于 2013-06-07T11:25:57.127 回答
0

listtemp.Clear() 是一种错误的语法,因此应将其删除,并且应在 for 循环中声明 listtemp,从而消除冗余和最初的问题

于 2013-06-08T21:39:37.187 回答