急救!!!求大神帮忙解决啊!!!
条件:
Levels:等级; Num:次数; No:排名;
DataTable dt = new DataTable();
dt.Columns.Add("Levels", typeof(string));
dt.Columns.Add("Num", typeof(string));
dt.Columns.Add("No", typeof(int));
DataRow dr1 = dt.NewRow();
dr1["Levels"] = "1";
dr1["Num"] = "7";
dt.Rows.Add(dr1);
dr1 = dt.NewRow();
dr1["Levels"] = "1";
dr1["Num"] = "5";
dt.Rows.Add(dr1);
dr1 = dt.NewRow();
dr1["Levels"] = "1";
dr1["Num"] = "3";
dt.Rows.Add(dr1);
dr1 = dt.NewRow();
dr1["Levels"] = "2";
dr1["Num"] = "6";
dt.Rows.Add(dr1);
dr1 = dt.NewRow();
dr1["Levels"] = "2";
dr1["Num"] = "4";
dt.Rows.Add(dr1);
dr1 = dt.NewRow();
dr1["Levels"] = "3";
dr1["Num"] = "0";
dt.Rows.Add(dr1);
dr1 = dt.NewRow();
dr1["Levels"] = "3";
dr1["Num"] = "9";
dr1 = dt.NewRow();
dr1["Levels"] = "3";
dr1["Num"] = "3";
dr1 = dt.NewRow();
dr1["Levels"] = "3";
dr1["Num"] = "4";
dt.Rows.Add(dr1);
问题 : 需要对dt进行同一等级下按次数进行排名,然后需要把排名的名次插入到dt当中
结果 : 排名 等级 次数
1 1 7
2 1 5
3 1 3
1 2 6
2 2 4
1 3 9
2 3 4
3 3 3
4 3 0
[解决办法]
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("Levels", typeof(string));
dt.Columns.Add("Num", typeof(string));
dt.Columns.Add("No", typeof(int));
DataRow dr1 = dt.NewRow();
dr1["Levels"] = "1";
dr1["Num"] = "7";
dt.Rows.Add(dr1);
dr1 = dt.NewRow();
dr1["Levels"] = "1";
dr1["Num"] = "5";
dt.Rows.Add(dr1);
dr1 = dt.NewRow();
dr1["Levels"] = "1";
dr1["Num"] = "3";
dt.Rows.Add(dr1);
dr1 = dt.NewRow();
dr1["Levels"] = "2";
dr1["Num"] = "6";
dt.Rows.Add(dr1);
dr1 = dt.NewRow();
dr1["Levels"] = "2";
dr1["Num"] = "4";
dt.Rows.Add(dr1);
dr1 = dt.NewRow();
dr1["Levels"] = "3";
dr1["Num"] = "0";
dt.Rows.Add(dr1);
dr1 = dt.NewRow();
dr1["Levels"] = "3";
dr1["Num"] = "9";
dt.Rows.Add(dr1);
dr1 = dt.NewRow();
dr1["Levels"] = "3";
dr1["Num"] = "3";
dt.Rows.Add(dr1);
dr1 = dt.NewRow();
dr1["Levels"] = "3";
dr1["Num"] = "4";
dt.Rows.Add(dr1);
var query = dt.Rows.Cast<DataRow>()
.GroupBy(x => int.Parse(x["Levels"].ToString()))
.OrderBy(x => x.Key)
.SelectMany(x => x.OrderByDescending(y => int.Parse(y["Num"].ToString())).Select((y, i) => new { y, i }));
DataTable dt1 = new DataTable();
dt1.Columns.Add("Levels", typeof(string));
dt1.Columns.Add("Num", typeof(string));
dt1.Columns.Add("No", typeof(int));
DataRow dr2 = dt1.NewRow();
foreach (var item in query)
{
dr2 = dt1.NewRow();
dr2["Levels"] = item.y["Levels"];
dr2["Num"] = item.y["Num"];
dr2["No"] = item.i + 1;
dt1.Rows.Add(dr2);
}
dt = dt1;
foreach (DataRow item in dt.Rows)
{
Console.WriteLine("{0}\t{1}\t{2}", item["No"], item["Levels"], item["Num"]);
}
}
}
}
--前端html代码
<table cellpadding="0" cellspacing="0" border="0" class="pagerlist" id="MasterDataList">
<tr >
<td>
排名
</td>
<td>
等级
</td>
<td>
次数
</td>
</tr>
<asp:Repeater runat="server" ID="repIndvCourseBooking" ViewStateMode="Disabled">
<ItemTemplate>
<tr class="row" >
<td>
<%#Eval("td1")%>
</td>
<td>
<%#Eval("td2")%>
</td>
<td>
<%#Eval("td3")%>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="altrow">
<td>
<%#Eval("td1")%>
</td>
<td>
<%#Eval("td2")%>
</td>
<td>
<%#Eval("td3")%>
</td>
</tr>
</AlternatingItemTemplate>
</asp:Repeater>
</table>
--后台查询与绑定
List<Obj> list = BookingSRV.GetObjList();
this.repIndvCourseBooking.DataSource = list.Select(t =>
{
return new
{
td0 = t.Levels,
td1 = t.Num,
td2 = t.No,
};
}).OrderByDescending(o=>o.td5);
this.repIndvCourseBooking.DataBind();