linq对datatable多列排序

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<DataRow> 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语句

作者: lo20067   发布时间: 2011-06-17

var list = source.OrderBy(...);

if (条件1)
  list = list.ThenBy(...);
if (条件2)
  list = list.ThenBy(...);
...

作者: caozhy   发布时间: 2011-06-17