Dictionary<string, List<string>> ?

解决方案 »

  1.   


                Dictionary<string, List<string>> list = new Dictionary<string, List<string>>();
                list.Add("1楼", new List<string>() { "桌子", "椅子", "板凳" });
                list.Add("2楼", new List<string>() { "桌子", "椅子", "板凳" });
                list.Add("3楼", new List<string>() { "桌子", "椅子", "板凳" });            Dictionary<string, List<string>> new_list = new Dictionary<string, List<string>>();
                List<string> tables = new List<string>();
                List<string> chairs = new List<string>();
                foreach (KeyValuePair<string, List<string>> l in list)
                {
                    if (l.Value.Contains("桌子"))
                    {
                        tables.Add(l.Key);
                    }
                    if (l.Value.Contains("椅子"))
                    {
                        chairs.Add(l.Key);
                    }
                }
                new_list.Add("桌子", tables);
                new_list.Add("椅子", chairs);用linq什么的好像就会很简单吧。。不过我不会linq
    感觉我这个应该比较菜
      

  2.   

    总感觉建个datatable会好
    比如用列表示物品,用行表示楼层
    1表示有,0表示没有
    如果发现一个没有定义过的物品,就动态添加一列
    最后列数就是物品的种类数
    然后就可以取得每一列对应行是否有内容
      

  3.   


                string[][] 物品 = new string[][]{
                    new string[]{"桌子","椅子","板凳","茶几"},
                    new string[]{"桌子","椅子","衣柜"},
                    new string[]{"桌子","椅子","板凳"},
                    new string[]{"桌子","椅子","板凳"},
                    new string[]{"桌子","椅子","板凳"},
                    new string[]{"桌子","椅子","板凳"},
                };
                DataTable dt = new DataTable();
                for (int i = 0; i < 物品.Length; i++)
                {
                    DataRow dr = dt.NewRow();
                    for (int j = 0; j < 物品[i].Length; j++)
                    {
                        if (!dt.Columns.Contains(物品[i][j]))
                        {
                            dt.Columns.Add(物品[i][j]);
                        }
                        dr[物品[i][j]] = 1;
                    }
                    dt.Rows.Add(dr);
                }放到datatable里之后,想知道1楼有什么,循环列1,去找行,有1的就是在该楼有,为空的就是该楼没有
      

  4.   

    这只是一个简单的横竖问题,4楼代表了我的看法,我对于数据源最喜欢的就是操作DATATABLE
      

  5.   

    用datatable去存,以后甚至可以知道每一楼层有桌子的数量是几个            string[][] 物品 = new string[][]{
                    new string[]{"桌子","椅子","板凳","茶几"},
                    new string[]{"桌子","椅子","衣柜"},
                    new string[]{"桌子","桌子","桌子","椅子","板凳"},
                    new string[]{"桌子","椅子","板凳"},
                    new string[]{"桌子","椅子","板凳"},
                    new string[]{"桌子","椅子","板凳"},
                };
                DataTable dt = new DataTable();
                for (int i = 0; i < 物品.Length; i++)
                {
                    DataRow dr = dt.NewRow();
                    for (int j = 0; j < 物品[i].Length; j++)
                    {
                        if (!dt.Columns.Contains(物品[i][j]))
                        {
                            dt.Columns.Add(物品[i][j]);
                        }                    if (dr[物品[i][j]].ToString().Length > 0)
                        {
                            dr[物品[i][j]] = Convert.ToInt32(dr[物品[i][j]]) + 1;
                        }
                        else
                        {
                            dr[物品[i][j]] = 1;
                        }
                    }
                    dt.Rows.Add(dr);
                }这样,空就是没有,有1个就是1,有2个就是2