当前位置: 代码迷 >> ASP.NET >> C# DataTable排序有关问题
  详细解决方案

C# DataTable排序有关问题

热度:6290   发布时间:2013-02-25 00:00:00.0
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
  相关解决方案