0
我已經得到了我需要的日期在括號訂購以下列名:如何訂購DataColumns?
「姓名」,「年齡」,「ABC(5/2010)」,「DEF(12/2010)「」efa(5/2011)「」ace(12/2011)「
如何使用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);
}
哦,你好,我剛在你發佈的同一時間找到答案。 – 2012-01-29 21:07:05
無論如何您都可以標記爲已接受。 :-) – 2012-01-29 21:08:36