首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

C# DataTable排序有关问题

2012-03-20 
C# DataTable排序问题我从数据库查询出来的数据格式为id12121212111111我在c#里面用datatable来接受它,并

C# DataTable排序问题
我从数据库查询出来的数据格式为

  id

  12
  12
  12
  12
  11
  11
  11

我在c#里面用datatable来接受它,并且根据元素出现重复的次数来排序,结果为:12,11,因为12出现4次,而11出现三次,所以12排在11的前面,明白嘛?

多谢!!

[解决办法]
那你查询时,sql中增加一个信息,count(*) group by id ,然后根据这个值排序
[解决办法]

SQL code
declare  @table  table(id int)insert into @table select 12 union allselect 11 union allselect 11 union allselect 12 union allselect 12 union allselect 12select list.* from @table listLEFT JOIN(    select id,COUNT(1) cou from @table group by id ) T ON T.id = list.idorder by T.cou DESC/*(6 行受影响)id-----------121212121111(6 行受影响)*/
[解决办法]
探讨
我从数据库查询出来的数据格式为

id

12
12
12
12
11
11
11

我在c#里面用datatable来接受它,并且根据元素出现重复的次数来排序,结果为:12,11,因为12出现4次,而11出现三次,所以12排在11的前面,明白嘛?

多谢!!

[解决办法]
SQL code
create table #table (id int)insert into #table select 12 union allselect 11 union allselect 11 union allselect 12 union allselect 12 union allselect 12select id,count(1) as c from #table group by id order by c desc/*12    411    2*/
[解决办法]
SQL code
select id,COUNT(id) from @table group by id ORDER BY COUNT(id) DESC 

热点排行