C# 具有多个排序条件的排序System.Data.DataTable
我有这个C# 具有多个排序条件的排序System.Data.DataTable,c#,linq,sorting,datatable,C#,Linq,Sorting,Datatable,我有这个System.Data.DataTable: DataTable dt = new DataTable(); dt.Columns.Add("Sector", typeof(string)); dt.Columns.Add("BBG IPC", typeof(double)); dt.Columns.Add("Analyst", typeof(string)); dt.Columns.Add("Issuer Group
System.Data.DataTable
:
DataTable dt = new DataTable();
dt.Columns.Add("Sector", typeof(string));
dt.Columns.Add("BBG IPC", typeof(double));
dt.Columns.Add("Analyst", typeof(string));
dt.Columns.Add("Issuer Group", typeof(string));
dt.Columns.Add("Seniority", typeof(string));
dt.Columns.Add("Mkt Value", typeof(double));
dt.Columns.Add("Nom Value", typeof(double));
dt.Columns.Add("Issue Group Rating", typeof(string));
dt.Columns.Add("Current SBR", typeof(string));
dt.Columns.Add("Notches", typeof(int));
dt.Columns.Add("Forward SBR", typeof(string));
dt.Columns.Add("Probability of Downgrade", typeof(string));
dt.Columns.Add("Risk Category", typeof(string));
dt.Columns.Add("Comment", typeof(string));
dt.Columns.Add("Restricted Message", typeof(string));
dt.Rows.Add(new object[] { "Agencies", 180969, "MUSTO", "Caisse des Depots", "Senior", 10114481, 1000000, "AA", "BB",-1, "YY", "Hight", "R1", "Comment", "gvbhjnnijnibj" });
dt.Rows.Add(new object[] { "Agencies", 180969, "MUSTO", "Caisse des Depots", "Senior", 10114481, 1000000, "AA", "CC",2, "II", "Low", "R2", "Bergtrgrt", "Other" });
dt.Rows.Add(new object[] { "Agencies", 180969, "MUSTO", "Caisse des Depots", "Senior", 10114481, 1000000, "AA", "EE",3, "LL", "Mid", "R1", "vggvbhjjnnjoioion", "ggvbhibniujbuhvg uvvugvghV" });
dt.Rows.Add(new object[] { "Consumer", 180969, "MUSTO", "Caisse des Depots", "Senior", 10114481, 1000000, "AA", "OO",-1, "SS", "Higth", "R3", "vgvgyhvgvjhbkj", "bibhjbhjbjhb ubuyhbuyhb hbuhbuhbhb" });
dt.Rows.Add(new object[] { "Energy", 180969, "MUSTO", "Caisse des Depots", "Senior", 10114481, 1000000, "AA", "PP",-2, "QQ","Higth", "R1", "ertyuiop", "tfcgvhb Uvugvugvuhvh" });
dt.Rows.Add(new object[] { "Energy", 180969, "MUSTO", "Caisse des Depots", "Senior", 10114481, 1000000, "AA", "GG",-3, "FF", "Low", "R2", "gvgvgvvgfccfdxdxrtf xrtfvgh tdctfgv trcfygvh tcfyghv b ygvhb ", "ygvgubiujb" });
我需要根据以下规则对表格进行排序:
第一级:按字母顺序排列的扇区
第二级:降级概率,按以下顺序排列:高、中、低
第三级:按字母顺序排列的发卡机构组
我曾经尝试过类似的
dt.DefaultView.Sort(“部门”、“降级概率”、“发行人组”)代码>但不起作用。我怎么做?谢谢大家! DataView.Sort属性的语法与SQL Server中使用的语法非常相似
对于您的示例,您可以使用以下字符串:
dt.DefaultView.Sort = "Sector ASC, [Probability of Downgrade] ASC, [Issuer Group] ASC";
。。然后只需在代码中访问dt.DefaultView
但您可能会遇到一个问题,因为升序排序(“High”、“Mid”、“Low”)将返回“High”、“Low”、“Mid”。因为它们是字符串,所以会按字母顺序排列
最简单的解决方案是使用概率的数字表示(如果源数据库中有),并显示高/中/低,但不按它们排序
如果您仅有的数据选项是概率字符串,那么这里有一个LINQ解决方案:
// Make a dictionary to map your probability strings to integers.
var probLookup = new Dictionary<string, int>();
probLookup["High"] = 3;
probLookup["Mid"] = 2;
probLookup["Low"] = 1;
// Convert your DataTable into an array of a new type, that has the DataRow
// plus the three things you need to sort by, as separate properties.
var temp = dt.Select().Select(row => new() {
Row = row,
Sector = row["Sector"],
// Map the downgrade prob. to an integer.
DowngradeProb = probLookup[(string) row["Probability of Downgrade"]],
IssuerGroup = row["Issuer Group"]
});
// Now, sort it by the 3 properties we created.
var temp2 = temp
.OrderBy(a => a.Sector)
.ThenByDesc(a => a.DowngradeProb)
.ThenBy(a => a.IssuerGroup);
// Now, fish out the Row again.
var temp3 = temp2.Select(a => a.Row).ToArray();
//制作一个字典,将概率字符串映射到整数。
var probLookup=新字典();
probLookup[“高”]=3;
probLookup[“Mid”]=2;
probLookup[“低”]=1;
//将DataTable转换为具有DataRow的新类型数组
//加上你需要排序的三件事,作为单独的属性。
var temp=dt.Select().Select(行=>new(){
行=行,
扇区=行[“扇区”],
//将降级问题映射为整数。
降级概率=probLookup[(字符串)行[“降级概率”],
IssuerGroup=行[“发卡机构组”]
});
//现在,按照我们创建的3个属性对其进行排序。
var temp2=温度
.OrderBy(a=>a.Sector)
.ThenByDesc(a=>a.降级概率)
.ThenBy(a=>a.IssuerGroup);
//现在,再钓一次。
var temp3=temp2.Select(a=>a.Row.ToArray();
重要免责声明:LINQ是凭记忆完成的:我不保证 DataView.Sort属性的语法与SQL Server中使用的语法非常相似
对于您的示例,您可以使用以下字符串:
dt.DefaultView.Sort = "Sector ASC, [Probability of Downgrade] ASC, [Issuer Group] ASC";
。。然后只需在代码中访问dt.DefaultView
但您可能会遇到一个问题,因为升序排序(“High”、“Mid”、“Low”)将返回“High”、“Low”、“Mid”。因为它们是字符串,所以会按字母顺序排列
最简单的解决方案是使用概率的数字表示(如果源数据库中有),并显示高/中/低,但不按它们排序
如果您仅有的数据选项是概率字符串,那么这里有一个LINQ解决方案:
// Make a dictionary to map your probability strings to integers.
var probLookup = new Dictionary<string, int>();
probLookup["High"] = 3;
probLookup["Mid"] = 2;
probLookup["Low"] = 1;
// Convert your DataTable into an array of a new type, that has the DataRow
// plus the three things you need to sort by, as separate properties.
var temp = dt.Select().Select(row => new() {
Row = row,
Sector = row["Sector"],
// Map the downgrade prob. to an integer.
DowngradeProb = probLookup[(string) row["Probability of Downgrade"]],
IssuerGroup = row["Issuer Group"]
});
// Now, sort it by the 3 properties we created.
var temp2 = temp
.OrderBy(a => a.Sector)
.ThenByDesc(a => a.DowngradeProb)
.ThenBy(a => a.IssuerGroup);
// Now, fish out the Row again.
var temp3 = temp2.Select(a => a.Row).ToArray();
//制作一个字典,将概率字符串映射到整数。
var probLookup=新字典();
probLookup[“高”]=3;
probLookup[“Mid”]=2;
probLookup[“低”]=1;
//将DataTable转换为具有DataRow的新类型数组
//加上你需要排序的三件事,作为单独的属性。
var temp=dt.Select().Select(行=>new(){
行=行,
扇区=行[“扇区”],
//将降级问题映射为整数。
降级概率=probLookup[(字符串)行[“降级概率”],
IssuerGroup=行[“发卡机构组”]
});
//现在,按照我们创建的3个属性对其进行排序。
var temp2=温度
.OrderBy(a=>a.Sector)
.ThenByDesc(a=>a.降级概率)
.ThenBy(a=>a.IssuerGroup);
//现在,再钓一次。
var temp3=temp2.Select(a=>a.Row.ToArray();
重要免责声明:LINQ是凭记忆完成的:我不保证 尝试以下与上次请求类似的操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
List<string> probabilityStr = new List<string>() { "Hight", "Mid", "Low" };
DataTable dt = new DataTable();
dt.Columns.Add("Sector", typeof(string)); //1
dt.Columns.Add("BBG IPC", typeof(double));
dt.Columns.Add("Analyst", typeof(string));
dt.Columns.Add("Issuer Group", typeof(string)); //3
dt.Columns.Add("Seniority", typeof(string));
dt.Columns.Add("Mkt Value", typeof(double));
dt.Columns.Add("Nom Value", typeof(double));
dt.Columns.Add("Issue Group Rating", typeof(string));
dt.Columns.Add("Current SBR", typeof(string));
dt.Columns.Add("Notches", typeof(int));
dt.Columns.Add("Forward SBR", typeof(string));
dt.Columns.Add("Probability of Downgrade", typeof(string)); //2
dt.Columns.Add("Risk Category", typeof(string));
dt.Columns.Add("Comment", typeof(string));
dt.Columns.Add("Restricted Message", typeof(string));
dt.Rows.Add(new object[] { "Agencies", 180969, "MUSTO", "Caisse des Depots", "Senior", 10114481, 1000000, "AA", "BB", -1, "YY", "Hight", "R1", "Comment", "gvbhjnnijnibj" });
dt.Rows.Add(new object[] { "Agencies", 180969, "MUSTO", "Caisse des Depots", "Senior", 10114481, 1000000, "AA", "CC", 2, "II", "Low", "R2", "Bergtrgrt", "Other" });
dt.Rows.Add(new object[] { "Agencies", 180969, "MUSTO", "Caisse des Depots", "Senior", 10114481, 1000000, "AA", "EE", 3, "LL", "Mid", "R1", "vggvbhjjnnjoioion", "ggvbhibniujbuhvg uvvugvghV" });
dt.Rows.Add(new object[] { "Consumer", 180969, "MUSTO", "Caisse des Depots", "Senior", 10114481, 1000000, "AA", "OO", -1, "SS", "Higth", "R3", "vgvgyhvgvjhbkj", "bibhjbhjbjhb ubuyhbuyhb hbuhbuhbhb" });
dt.Rows.Add(new object[] { "Energy", 180969, "MUSTO", "Caisse des Depots", "Senior", 10114481, 1000000, "AA", "PP", -2, "QQ", "Higth", "R1", "ertyuiop", "tfcgvhb Uvugvugvuhvh" });
dt.Rows.Add(new object[] { "Energy", 180969, "MUSTO", "Caisse des Depots", "Senior", 10114481, 1000000, "AA", "GG", -3, "FF", "Low", "R2", "gvgvgvvgfccfdxdxrtf xrtfvgh tdctfgv trcfygvh tcfyghv b ygvhb ", "ygvgubiujb" });
DataTable dt2 = dt.Clone();
var sectors = dt.AsEnumerable()
.OrderBy(x => x.Field<string>("Sector"))
.ThenBy(x => probabilityStr.IndexOf(x.Field<string>("Probability of Downgrade")))
.ThenBy(x => x.Field<string>("Issuer Group"))
.GroupBy(x => x.Field<string>("Sector"))
.ToList();
for (int si = 0; si < sectors.Count(); si++)
{
var probabilities = sectors[si].GroupBy(x => x.Field<string>("Probability of Downgrade")).ToList();
for (int pi = 0; pi < probabilities.Count(); pi++)
{
var groups = probabilities[pi].GroupBy(x => x.Field<string>("Issuer Group")).ToList();
for (int gr = 0; gr < groups.Count(); gr++)
{
for (int r = 0; r < groups[gr].Count(); r++)
{
DataRow row = dt2.Rows.Add();
for (int i = 0; i < dt2.Columns.Count; i++)
{
switch (dt2.Columns[i].ColumnName)
{
case "Sector" :
if (pi == 0)
{
row["Sector"] = sectors[si].Key;
}
else
{
row["Sector"] = DBNull.Value;
}
break;
case "Probability of Downgrade" :
if (gr == 0)
{
row["Probability of Downgrade"] = probabilities[pi].Key;
}
else
{
row["Probability of Downgrade"] = DBNull.Value;
}
break;
case "Issuer Group" :
if (r == 0)
{
row["Issuer Group"] = groups[gr].Key;
}
else
{
row["Issuer Group"] = DBNull.Value;
}
break;
default :
row[i] = groups[r].First()[dt2.Columns[i].ColumnName];
break;
}
}
}
}
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统数据;
命名空间控制台应用程序11
{
班级计划
{
静态void Main(字符串[]参数)
{
列表概率str=新列表(){“高”、“中”、“低”};
DataTable dt=新的DataTable();
dt.Columns.Add(“扇区”,typeof(字符串));//1
添加(“BBG IPC”,类型(双));
添加(“分析师”,类型(字符串));
dt.Columns.Add(“发卡机构组”,类型(字符串));//3
添加(“资历”,类型(字符串));
添加(“Mkt值”,类型(双));
添加(“标称值”,类型(双));
添加(“问题组评级”,类型(字符串));
添加(“当前SBR”,类型(字符串));
添加(“缺口”,类型(int));
添加(“正向SBR”,类型(字符串));
dt.Columns.Add(“降级概率”,typeof(string));//2
增加(“风险类别”,类型(字符串));
添加(“注释”,类型(字符串));
添加(“受限消息”,类型为(字符串));
添加(新对象[]{“代理”,180969,“MUSTO”,“Caisse des Depots”,“Senior”,101144811000000,“AA”,“BB”,-1,“YY”,“Hight”,“R1”,“Comment”,“GVBHJNIJNIBJ”});
添加(新对象[]{“机构”,180969,“MUSTO”,“车辆段”,“高级”,101144811000000,“AA”,“CC”,2,“II”,“低”,“R2”,“Bergtrgrt”,“其他”});
添加(新对象[]{“代理”,180969,“MUSTO”,“Caisse des Depots”,“Senior”,101144811000000,“AA”,“EE”,3,“LL”,“Mid”,“R1”