C# 同分數排名相同
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
DataTable dt = p.getDt();
DataTable dt_order = (from temp_dt in dt.AsEnumerable()
orderby temp_dt.Field<decimal>("score_sum") descending
select temp_dt).CopyToDataTable();
dt_order.Columns.Add("rank", typeof(int));
dt_order.Columns.Add("save_ank", typeof(int));
int rank = 0;
int save_rank = 0;
decimal tmp = 0;
foreach (DataRow dr in dt_order.Rows)
{
rank++;
Decimal score_sum = Decimal.Parse(dr["score_sum"].ToString());
if(score_sum == tmp){
save_rank = save_rank;
}else{
save_rank = rank;
}
tmp = score_sum;
dr["rank"] = rank;
dr["save_ank"] = save_rank;
}
DataTable show = dt_order;
}
public DataTable getDt() {
SqlDataAdapter apt;
DataTable dt;
String sql = "";
string str = "Data Source=資料庫主機;Initial Catalog=資料庫;Integrated Security=SSPI";
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("", con);
#region sql
con.Open();
sql = @"
select
資料庫.dbo.score_sum
from
myTable
";
cmd.CommandText = sql;
apt = new SqlDataAdapter(cmd);
dt = new DataTable();
apt.Fill(dt);
#endregion
return dt;
}
}
}
資料來源: