从数据库中 select 到一张表列1 列2
a   a-1
a   a-2
c   c-2
a   a-3
c   c-1
a   a-5
b   b-3
c   c-1
b   b-2
b   b-1我想实现,将在“列1”中相同的 “列2” 的值都保存在一个数组里头上表中的“列1” 有三个 不同项“a”“b”“c”; 列2 则是按列1 相同与否 分别到 a[],b[],c[] 数组中;请问各位亲,我该怎么实现呢???

解决方案 »

  1.   

    先将表放到dataset里:
    然后用个for 语句  if 判断 第一列=a
    a[i] = 对应列
    同理判断B,c就可以了吧
      

  2.   

    对,这个是很笨的方法。 其实我是想做 treeview 里头的node是的,好像递归啥的办法挺好的。可是我不懂递归。。各位亲。。怎么搞。我需要一个高效的。
      

  3.   

    本帖最后由 caozhy 于 2012-04-21 19:10:12 编辑
      

  4.   


    只要你学一下linq,这是最基本的代码。为什么总是“似懂非懂”呢?要么你就干脆说“我就是不喜欢Linq!”,要么你就认真花3天时间学习一下。初学学习linq可以从这个页面作为主要参考:http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
      

  5.   

    dictionary(of string,list(of string))
      

  6.   

    dim list as new dictionary(of string,list(of string))循环数据库数据
    if list.ContainsKey("列1") then
    list("列1").add("列2")
    else
    list("列1")=new list(of string)
    list("列1").add("列2")
    end
      

  7.   

    呀  这里是c#的  自己转换以下吧  我这个是vb的 嘿嘿
      

  8.   

    感觉 rayyu1989 的代码很精悍哦。不知道是否可以。dim list as new dictionary(of string,list(of string))
    是下面这个意思吗??
    dictionary<string s,list> list = new dictionary<string s,list>;
      

  9.   

    感觉 rayyu1989 的代码很精悍哦。不知道是否可以。dim list as new dictionary(of string,list(of string))
    是下面这个意思吗??
    dictionary<string s,list> list = new dictionary<string s,list>();
      

  10.   

    本帖最后由 caozhy 于 2012-04-21 22:22:03 编辑
      

  11.   


    谢谢,看来有必要学习下,Linq 了,明天我试试。。
      

  12.   


    给你转了下 
    Dictionary<string,List<string>> list = new Dictionary<string,List<string>>();            //循环数据库数据
                if(list.ContainsKey ("列1")){
                    list["列1"].Add("列2");
                }else{
                     list["列1"]=new List<string>();
                     list["列1"].Add("列2");
                }
      

  13.   


    楼上兄弟,您好!我得到了这个 list 后,如果需要生成 treeview 的nodes 的话,那就是再来一个循环,一个一个取值画上去,对吗??
      

  14.   

    Dictionary   for each  至于list  可以join 嘛
      

  15.   

     var source = from temp in dt.AsEnumerable()
                             group temp by temp.Field<string>("列1") into g
                             select new
                                 {
                                     key = g.Key,
                                     g,
                                     values = g.Select(t => t.Field<string>("列2")).ToArray()
                                 };
               foreach (var t in source)
               {
                                   string[] str=  t.values;
               }
      

  16.   


    谢谢,使用了rayyu1989 办法整了下,感觉比较笨。感谢感谢。
    分有点少,我花点心思给你加。一个一个加。