C# 筛选问题提问

lisweet_win 2012-04-20 04:19:56

从数据库中 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[] 数组中;

请问各位亲,我该怎么实现呢???
...全文
295 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
lisweet_win 2012-04-24
  • 打赏
  • 举报
回复
网络慢,白天再结贴了。谢谢。。。
lisweet_win 2012-04-24
  • 打赏
  • 举报
回复

谢谢,使用了rayyu1989 办法整了下,感觉比较笨。感谢感谢。
分有点少,我花点心思给你加。一个一个加。
lisweet_win 2012-04-23
  • 打赏
  • 举报
回复
都是 linq 高手。。。

  • 打赏
  • 举报
回复
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;
}
rayyu1989 2012-04-22
  • 打赏
  • 举报
回复
Dictionary for each

至于list 可以join 嘛
lisweet_win 2012-04-22
  • 打赏
  • 举报
回复

楼上兄弟,您好!

我得到了这个 list 后,如果需要生成 treeview 的nodes 的话,那就是再来一个循环,一个一个取值画上去,对吗??
rayyu1989 2012-04-22
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 的回复:]

感觉 rayyu1989 的代码很精悍哦。

不知道是否可以。

dim list as new dictionary(of string,list(of string))
是下面这个意思吗??
dictionary<string s,list> list = new dictionary<string s,list>();
[/Quote]

给你转了下

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");
}
lisweet_win 2012-04-21
  • 打赏
  • 举报
回复

谢谢,看来有必要学习下,Linq 了,明天我试试。。。。。
threenewbee 2012-04-21
  • 打赏
  • 举报
回复
如果你要转换成字典,用linq还是一行。
var query = from y in (from x in DataTable.Cast<DataRow>()
select new { a = x["列1"].ToString(), b = x["列2"].ToString() })
group y by y.a into g
select new { key = g.Key, values = g.Select(z => z.b).ToArray() };
Dictionary<string, List<string>> list = query.ToDictionary(x => x.key, x => x.values.ToList());
lisweet_win 2012-04-21
  • 打赏
  • 举报
回复
感觉 rayyu1989 的代码很精悍哦。

不知道是否可以。

dim list as new dictionary(of string,list(of string))
是下面这个意思吗??
dictionary<string s,list> list = new dictionary<string s,list>();
lisweet_win 2012-04-21
  • 打赏
  • 举报
回复
感觉 rayyu1989 的代码很精悍哦。

不知道是否可以。

dim list as new dictionary(of string,list(of string))
是下面这个意思吗??
dictionary<string s,list> list = new dictionary<string s,list>;

lisweet_win 2012-04-21
  • 打赏
  • 举报
回复

可以哦。。试试。。。
rayyu1989 2012-04-21
  • 打赏
  • 举报
回复
呀 这里是c#的 自己转换以下吧 我这个是vb的 嘿嘿
rayyu1989 2012-04-21
  • 打赏
  • 举报
回复
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
rayyu1989 2012-04-21
  • 打赏
  • 举报
回复
dictionary(of string,list(of string))
orochiheart 2012-04-21
  • 打赏
  • 举报
回复
呵呵 感谢sp大神的回复!说的有道理啊~!可能是和之前的语法有太大不同吧 自己有一些惰性 不过你既然这么说了 我觉得确实应该考虑去花时间研究研究了
  • 打赏
  • 举报
回复
学习不是靠别人给你写代码的方式来学习的,学习是要自己下一点苦功夫的。
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

天哪 什么时候能够我楼上的代码 总是那种似懂非懂的感觉
[/Quote]

只要你学一下linq,这是最基本的代码。为什么总是“似懂非懂”呢?要么你就干脆说“我就是不喜欢Linq!”,要么你就认真花3天时间学习一下。

初学学习linq可以从这个页面作为主要参考:http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
orochiheart 2012-04-21
  • 打赏
  • 举报
回复
天哪 什么时候能够我楼上的代码 总是那种似懂非懂的感觉
threenewbee 2012-04-21
  • 打赏
  • 举报
回复
用Linq

var query = from y in (from x in DataTable.Cast<DataRow>()
select new { a = x["列1"].ToString(), b = x["列2"].ToString() })
group y by y.a into g
select new { key = g.Key, values = g.Select(z => z.b).ToArray() };
foreach (var item in query)
{
string name = item.key;
string[] values = item.values;
...//你可以处理,已经归类放入数组了
}
加载更多回复(7)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧