从数据库中 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[] 数组中;请问各位亲,我该怎么实现呢???
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
呀 这里是c#的 自己转换以下吧 我这个是vb的 嘿嘿
感觉 rayyu1989 的代码很精悍哦。不知道是否可以。dim list as new dictionary(of string,list(of string)) 是下面这个意思吗?? dictionary<string s,list> list = new dictionary<string s,list>;
感觉 rayyu1989 的代码很精悍哦。不知道是否可以。dim list as new dictionary(of string,list(of string)) 是下面这个意思吗?? dictionary<string s,list> list = new dictionary<string s,list>();
本帖最后由 caozhy 于 2012-04-21 22:22:03 编辑
谢谢,看来有必要学习下,Linq 了,明天我试试。。
给你转了下 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"); }
楼上兄弟,您好!我得到了这个 list 后,如果需要生成 treeview 的nodes 的话,那就是再来一个循环,一个一个取值画上去,对吗??
Dictionary for each 至于list 可以join 嘛
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; }
然后用个for 语句 if 判断 第一列=a
a[i] = 对应列
同理判断B,c就可以了吧
只要你学一下linq,这是最基本的代码。为什么总是“似懂非懂”呢?要么你就干脆说“我就是不喜欢Linq!”,要么你就认真花3天时间学习一下。初学学习linq可以从这个页面作为主要参考:http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
if list.ContainsKey("列1") then
list("列1").add("列2")
else
list("列1")=new list(of string)
list("列1").add("列2")
end
是下面这个意思吗??
dictionary<string s,list> list = new dictionary<string s,list>;
是下面这个意思吗??
dictionary<string s,list> list = new dictionary<string s,list>();
谢谢,看来有必要学习下,Linq 了,明天我试试。。
给你转了下
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");
}
楼上兄弟,您好!我得到了这个 list 后,如果需要生成 treeview 的nodes 的话,那就是再来一个循环,一个一个取值画上去,对吗??
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;
}
谢谢,使用了rayyu1989 办法整了下,感觉比较笨。感谢感谢。
分有点少,我花点心思给你加。一个一个加。