linq对datatable多列排序,该怎么处理

 我来答
茄子猫1240
2013-10-17 · 超过58用户采纳过TA的回答
知道答主
回答量:118
采纳率:0%
帮助的人:51.8万
展开全部
C# code private void TestSortLinq() { string idColumnName = "ID"; string nameColumnName = "Name"; string phoneColumnName="Phone"; string addressColumnName="Address"; DataTable testDT = new DataTable(); testDT.Columns.AddRange( new DataColumn[] { new DataColumn(idColumnName,typeof(System.String)), new DataColumn(nameColumnName,typeof(System.String)), new DataColumn(phoneColumnName,typeof(System.String)), new DataColumn(addressColumnName,typeof(System.String))}); for (int i = 0; i < 20; i++) { Random _rd = new Random(i); DataRow row = testDT.NewRow(); row[idColumnName] = idColumnName + _rd.Next(i).ToString(); row[nameColumnName] = nameColumnName + _rd.Next(i).ToString(); row[phoneColumnName] = phoneColumnName + _rd.Next(i).ToString(); row[addressColumnName] = addressColumnName + _rd.Next(i).ToString(); testDT.Rows.Add(row); } testDT.TableName="testDT"; IEnumerable rows = from p in testDT.AsEnumerable() select p; #region 没问题 rows = rows.CopyToDataTable().AsEnumerable().OrderBy(s => decimal.Parse(Regex.Match(s[idColumnName].ToString(), @"\d+").Value)); testDT = rows.CopyToDataTable(); #endregion #region 有问题 rows = rows.CopyToDataTable().AsEnumerable().OrderBy(s => new { //虽然现在这里只写了两个,但是有可能是三个或者是四个或者是一个,请问这里怎么写成动态的orderby语句 id = decimal.Parse(Regex.Match(s[idColumnName].ToString(), @"\d+").Value), name = decimal.Parse(Regex.Match(s[nameColumnName].ToString(), @"\d+").Value) }); testDT = rows.CopyToDataTable(); #endregion } 大家看下我的代码就知道我说的什么了:1是为什么第二种写法会报错,2,如何写一个动态的orderby语句
------解决方案--------------------------------------------------------var list = source.OrderBy(...);if (条件1)list = list.ThenBy(...);if (条件2)list = list.ThenBy(...);
------解决方案--------------------------------------------------------C# code rows = rows.CopyToDataTable().AsEnumerable() .OrderBy(s =>decimal.Parse(Regex.Match(s[idColumnName].ToString(), @"\d+").Value)) .ThenBy(s =>decimal.Parse(Regex.Match(s[nameColumnName].ToString(), @"\d+").Value));
------解决方案-------------------------------------------------------- 用var 关键字!!!
var query = dt.AsEnumerable().OrderBy(r => 1);
OrderBy完是 OrderedEnumerableRowCollection
------解决方案-------------------------------------------------------- 给你贴个main函数,通过设置Flag和str_c来动态指定排序的列,你试试看对不对
注:"ID2"大于"ID10"。。。郁闷了我很久。。。
C# code static void Main(string[] args) { string idColumnName = "ID"; string nameColumnName = "Name"; string phoneColumnName = "Phone"; string addressColumnName = "Address"; DataTable testDT = new DataTable(); testDT.Columns.AddRange( new DataColumn[] { new DataColumn(idColumnName,typeof(System.String)), new DataColumn(nameColumnName,typeof(System.String)), new DataColumn(phoneColumnName,typeof(System.String)), new DataColumn(addressColumnName,typeof(System.String))}); for (int i = 0; i < 20; i++) { Random _rd = new Random(i); DataRow row = testDT.NewRow(); row[idColumnName] = idColumnName + _rd.Next(i).ToString(); row[nameColumnName] = nameColumnName + _rd.Next(i).ToString(); row[phoneColumnName] = phoneColumnName + _rd.Next(i).ToString(); row[addressColumnName] = addressColumnName + _rd.Next(i).ToString(); testDT.Rows.Add(row); } var query = from s in testDT.AsEnumerable() orderby new orderClass() { C1 = get_c1(s), C2 = get_c2(s), C3 = get_c3(s), C4 = get_c4(s) } select s; foreach (var r in query) { Console.Write(r[0].ToString() + " "); Console.Write(r[1].ToString() + " "); Console.Write(r[2].ToString() + " "); Console.Write(r[3].ToString() + " "); Console.WriteLine(); } Console.ReadKey(); } static private bool c1Flag = true;//控制是否依orderClass的第一项来排序 static private bool c2Flag = false; static private bool c3Flag = false; static private bool c4Flag = true; static private string str_c1 = "ID";//控制排序的第一项对应的 属性名称 static private string str_c2 = "Name"; static private string str_c3 = "Phone"; static private string str_c4 = "Address"; static private string get_c4(DataRow s) { //在函数内部进行判断,控制返回值 if (c4Flag) { return s[str_c4].ToString(); } else { return ""; } } static private string get_c3(DataRow s) { if (c3Flag) { return s[str_c3].ToString(); } else { return ""; } } static private string get_c2(DataRow s) { if (c2Flag) { return s[str_c2].ToString(); } else { return ""; } } static private string get_c1(DataRow s) { if (c1Flag) { return s[str_c1].ToString(); } else { return ""; } } class orderClass : IComparable { //c1, c2, c3, c4表示排序的优先顺序,当然,可以有c5,c6等等 private string c1, c2, c3, c4; public string C4 { get { return c4; } set { c4 = value; } } public string C3 { get { return c3; } set { c3 = value; } } public string C2 { get { return c2; } set { c2 = value; } } public string C1 { get { return c1; } set { c1 = value; } } #region IComparable 成员 int IComparable.CompareTo(orderClass p) { if (c1.CompareTo(p.c1) != 0) { return c1.CompareTo(p.c1); } else if (c2.CompareTo(p.c2) != 0) { return c2.CompareTo(p.c2); } else if (c3.CompareTo(p.c3) != 0) { return c3.CompareTo(p.c3); } else { return c4.CompareTo(p.c4); } } #endregion }
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式