C# 如何对数据列进行排序?

C# 如何对数据列进行排序?,c#,linq,.net-4.0,ado.net,C#,Linq,.net 4.0,Ado.net,我需要按括号中的日期排序以下列名: “姓名”、“年龄”、“abc(2010年5月)”、“def(2010年12月)”、“efa(2011年5月)”、“ace(2011年12月)” 如何使用LINQ订购这些 我试过这个方法。看起来我不应该对LINQ做这些: Dictionary<DataColumn, DateTime> columns = new Dictionary<DataColumn, DateTime>(); int startIndex; int endInde

我需要按括号中的日期排序以下列名:

“姓名”、“年龄”、“abc(2010年5月)”、“def(2010年12月)”、“efa(2011年5月)”、“ace(2011年12月)”

如何使用LINQ订购这些

我试过这个方法。看起来我不应该对LINQ做这些:

Dictionary<DataColumn, DateTime> columns = new Dictionary<DataColumn, DateTime>();
int startIndex;
int endIndex;
for (int i = 0; i < table.Columns.Count; ++i)
{        
    // these columns need to be sorted
    startIndex = table.Columns[i].Caption.IndexOf('(');
    endIndex = table.Columns[i].Caption.IndexOf(')');
    if (startIndex > 0 && endIndex > 0)
    {
        // create a standard date
        string monthYear = table.Columns[i].Caption.Substring(
            startIndex + 1, endIndex - startIndex - 1);
        columns.Add(
            table.Columns[i], 
            DateTime.Parse(monthYear.Replace("/", "/01/")).Date);
    }
    else
    {
        // all other columns should be sorted first
        columns.Add(table.Columns[i], DateTime.MinValue);
    }
}

// perform the LINQ order by
columns = columns.OrderBy(o => o.Value).ToDictionary(o => o.Key, o => o.Value);

// order the original table uses the dictionary
for (int i = 0; i < columns.Count; ++i)
{
    table.Columns[columns.Keys.ElementAt(i).Caption].SetOrdinal(i);
}
Dictionary columns=newdictionary();
国际标准指数;
内部索引;
对于(int i=0;i0&&endIndex>0)
{
//创建一个标准日期
string monthYear=table.Columns[i].Caption.Substring(
startIndex+1,endIndex-startIndex-1);
列。添加(
表.第[i]列,
Parse(monthYear.Replace(“/”,“/01/”).Date);
}
其他的
{
//应首先对所有其他列进行排序
columns.Add(table.columns[i],DateTime.MinValue);
}
}
//通过执行LINQ命令
columns=columns.OrderBy(o=>o.Value).ToDictionary(o=>o.Key,o=>o.Value);
//排序原始表使用字典
对于(int i=0;i
OrderBy.ThenBy无法正常工作。您需要将结果分配给另一个变量。

哦,是的,我刚刚在您发布的同时找到了答案。