下面这个是多维数据表,命名表1(数据未全部列出)
姓名 种类
陈晓婷 I
陈晓婷 Z
陈晓婷 K
陈晓婷 H
陈晓婷 S
陈晓婷 J
祝欣 I
祝欣 K
祝欣 T
祝欣 R
祝欣 S
祝欣 B
。下面是单维数据表,命名为表2
姓名 种类 种类 种类 种类 种类 种类 种类 种类
陈晓婷 I Z K H S J 
祝欣 I K T R S B 
朱希强 H I O T K S J B
陈敏 I J S G T K H 
王会 S R 
乔倩 I G Z T S 
邝佩佩 B T S K 
何佩 I H F T K 
陶连飞 I K T R S 
郭玲玲 H I J B 
钱文予 Q T I K H J B 
唐珊珊 O那请问下用C#语言,将表1变为表2的伪代码应该如何写,请吧详细注释注明下,谢谢罗 

解决方案 »

  1.   


        class testclass
        {
            public string sName { get; set; }
            public string stype { get; set; }       
        }    class Program
        {
            static void Main(string[] args)
            {
                List<testclass> li = new List<testclass>
                {
                    new testclass{sName="AA",stype="a"},
                    new testclass{sName="AA",stype="b"},
                    new testclass{sName="AA",stype="c"},
                    new testclass{sName="AA",stype="d"},
                    new testclass{sName="AA",stype="e"},                new testclass{sName="BB",stype="e"},
                    new testclass{sName="BB",stype="f"},
                    new testclass{sName="BB",stype="m"},
                    new testclass{sName="BB",stype="i"},                new testclass{sName="CC",stype="d"},
                    new testclass{sName="CC",stype="c"},
                    new testclass{sName="CC",stype="e"},
                   
                   
                };
                //找出多少个人(不重复)
                var mypp = (from testclass in li
                            select new
                            {
                                testclass.sName
                            }).Distinct();            Console.Write("name    stype \r\n");
                foreach (var qq in mypp)
                {
                    testclass tc = new testclass();
                    string str = qq.sName;
                    //根据此人找出种类
                    var pp = (from testclass in li
                              where testclass.sName == str
                              select new
                              {
                                  testclass.stype
                              }).ToList();                Console.Write(str + "      ");
                    foreach (var dd in pp)
                    {
                        Console.Write(dd.stype );
                        
                    }
                    Console.Write("\r\n");
                }            Console.ReadLine();
            }
        }
      

  2.   


     class Program
        {
            static void Main(string[] args)
            {
                List<string[]> li = new List<string[]>();
                li.Add(new string[]{"AA","a"});
                li.Add(new string[] { "AA", "b" });
                li.Add(new string[] { "AA", "c" });
                li.Add(new string[] { "AA", "d" });            li.Add(new string[] { "BB", "a" });
                li.Add(new string[] { "BB", "b" });
                li.Add(new string[] { "BB", "d" });
                li.Add(new string[] { "BB", "f" });            li.Add(new string[] { "CC", "d" });
                li.Add(new string[] { "CC", "e" });
                li.Add(new string[] { "CC", "f" });
                li.Add(new string[] { "CC", "i" });            li.Add(new string[] { "DD", "a" });
                li.Add(new string[] { "DD", "d" });
                li.Add(new string[] { "DD", "f" });            //新集合
                List<ArrayList> linew = new List<ArrayList>();            for (int i = 0; i < li.Count; i++)
                {
                    //第一次添加到新集合
                    if (linew.Count == 0)
                    {
                        ArrayList ac = GetStypeList(li, li[i][0]);
                        linew.Add(ac);
                    }
                    else  //判读是否在新集合中,如果有跳出循环,继续
                    {
                        bool fg = false;
                        foreach (ArrayList ar in linew)
                        {
                            if (ar[0] == li[i][0])
                            {
                                fg = true;
                                break;
                            }
                        }
                        if (fg)
                        {
                            continue;
                        }
                        
                    }
                }
            }
            /// <summary>
            /// 根据名字获取所有种类
            /// </summary>
            /// <param name="li"></param>
            /// <param name="sName"></param>
            /// <returns></returns>
            public static ArrayList GetStypeList(List<string[]> li,string sName)
            {
                ArrayList al = new ArrayList();
                al.Add(sName);
                for (int i = 0; i < li.Count; i++)
                {
                    if (li[i][0] == sName)
                    {
                        al.Add(li[i][1]);
                    }
                }
                return al;        }
        }写的不是很优,请高手们指点。不知道你的集合有没有按名字排序,如果有排序的话,你自己优化吧。