我有是這樣的一個CSV文件:LINQ無法在特定列中訂購日期?
1,01/01/2001,a
2,19/09/2013,as
3,12/05/2016,asd
4,13/05/2016,asdf
5,12/12/2012,asdfg
6,05/02/2006,asdfgh
7,06/03/2011,asdfghj
我想排序和按時間順序顯示的日期,但我似乎無法讓我的代碼來梳理出來。但是,我的代碼能夠根據CSV文件中的格式顯示日期。
如果CSV文件是這樣的:
01/01/2001
19/09/2013
12/05/2016
13/05/2016
12/12/2012
05/02/2006
06/03/2011
然後,它工作得很好。
下面是我的代碼:
string parts = new StreamReader(@"C:\input.txt").ReadToEnd();
string[] file = parts.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < file.Length; i++)
{
string[] data = file[i].Split(new string[] { "," }, StringSplitOptions.None);
string[] dates = new string[] { data[1].ToString() };
//var date = dates.OrderBy(x => DateTime.Parse(x)).ToList();
var date = dates.OrderBy(x => DateTime.ParseExact(x, "dd/MM/yyyy", null));
foreach (string s in date)
{
sb.AppendLine(s + "<br />");
Label1.Text = sb.ToString();
}
}
我已經測試CSV文件同時使用DateTime.Parse
和DateTime.ParseExact
這個代碼,但他們沒有工作。
我是LINQ查詢的新手,這是我第一次使用它。如果有關於我的代碼有什麼問題的解釋,我會非常感激。
我想,你需要用逗號分割你的文件,然後按行日期列排序。 – SeM
發佈預期標籤文本與實際標籤文本。你的意思是什麼沒有奏效?用於排序的linq代碼似乎沒問題。 – user6144226
這裏的問題是你正在排序'每行'而不是排序所有行。 – user6144226