2014-08-27 108 views
1

我有一個應用程序讀取日期文本文件。如何排序和輸出存儲在列表中的數據

請注意,我仍然在學習C#

在文本文件中的內容是這樣的: 即我的內容沒有進行排序,但我已經將它們存儲在列表和ASC排序訂購。

02/10/1998 
03/10/1998 
07/10/1998 
10/10/1998 
17/10/1998 
20/10/1998 

文本文件還有更多的一百日期,由於這樣的事實,我使用的是手機,我無法輸入的所有文本,不能寫整個代碼。

基本的應用程序的目的是爲了寫一個新的文本文件,內容與**周塊從時間跨度 即:

Week : Sunday 27 1998 to Saturday 03 1998 
Friday 02 1998 
Fri 2 98 
02/10/98 
Saturday 03 1998 
Sat 2 98 
03/10/1998 
Week : Sunday 04 1998 to Saturday 10 1998 
Wednesday 07 1998 
Wed 7 98 
07/10/1998 
Saturday 10 1998 
Sat 10 98 
10/10/1998 

//And so on till last date 

輸出應該總是第一個星期日開始至星期六並在上週日至週六結束。

我只需要知道的是如何通過周時間段從時間段生成這樣的輸出?

我已經寫的代碼從列表進行排序,現在我想我要解析的日期格式

var sortedlist = stulist.OrderBy(s => s.DOB); 

DateTime dt = DateTime.ParseExact(line, "ddddMMMMdd", CultureInfo.InvariantCulture); 
DateTime dt2 = DateTime.ParseExact(line, "dddMMMd", CultureInfo.InvariantCulture); 
DateTime dt3 = DateTime.ParseExact(line, "MM/dd/yyyy", CultureInfo.InvariantCulture); 

我想我有循環,但我不能找出在我while循環指定

任何幫助將非常理解

會什麼區別:

tx.WriteLine(dt.ToString("dddd dd yyyy", CultureInfo.InvariantCulture)); 

有,如果我保持ParseExact?

tx.WriteLine(dt); 
+1

您應該編輯您的問題,並闡明你真的想知道如何從一個時間跨度讓那些一週塊。目前還不清楚這個或日期時間格式是否是您問題的核心。告知我 – 2014-08-27 15:00:20

+0

@TimSchmelter感謝,我editted。 – Brian02 2014-08-27 15:09:15

+0

你想擁有所有爲期一週的塊或僅針對您也有日期顯示? – 2014-08-27 15:49:52

回答

2

你的問題有點混亂。我試圖重述你可能在做的事情。你似乎有一個Student類別(其中包含)字段DOB。我認爲它是DateTime。然後,你必須做這樣的事情,以便閱讀文本文件:

var students = new List<Student>(); 
string[] lines = File.ReadAllLines("my file path"); 
CultureInfo southAfricanCuture = new CultureInfo("en-ZA"); 
foreach (string l in lines) { 
    DateTime d; 
    if (DateTime.TryParseExact(l, "dd/MM/yyyy", southAfricanCuture, 
           DateTimeStyles.AllowWhiteSpaces, out d)) 
    { 
     students.Add(new Student { DOB = d }); 
    } 
} 

我們需要爲了有一天和一個月的正確的順序使用TryParseExact。但也許我只是在使用錯誤的文化。

I.e.將文本提供的日期轉換爲DateTime時發生解析。爲了顯示日期,稍後,您將不得不通過格式化將它們轉換回string(不解析!)。

下去,我們需要這樣的輔助方法:

public static DateTime FirstDateOfWeek(DateTime date) 
{ 
    // Since Sunday = 0 and we want sunday to be the first day of week: 
    int dayOfWeek = (int)date.DayOfWeek; 
    return date.AddDays(-dayOfWeek); 
} 

現在,讓我們使用LINQ創建日期塊(確保你在你的代碼的開頭有一個using System.Linq;):

var dtfi = DateTimeFormatInfo.InvariantInfo; 
Calendar cal = dtfi.Calendar; 

var dateBlocks = students 
    .Select(s => s.DOB) 
    .GroupBy(d => 100 * d.Year + 
        cal.GetWeekOfYear(d, CalendarWeekRule.FirstDay, DayOfWeek.Sunday)) 
    .OrderBy(g => g.First()); // Order the groups by first date found in them. 

現在,讓我們創建輸出:

foreach (var block in dateBlocks) { 
    // Week header 
    DateTime firstDayOfWeek = FirstDateOfWeek(block.First()); 
    DateTime lastDayOfWeek = firstDayOfWeek.AddDays(6); 
    string weekHeader = String.Format(southAfricanCuture, 
     "Week : {0:dddd dd/MM yyyy} to {1:dddd dd/MM yyyy}", 
     firstDayOfWeek, lastDayOfWeek); 
    Console.WriteLine(weekHeader); 

    // Week details. 
    // If the dates appear in wrong order in the input file, 
    // order the days within the groups. 
    foreach (DateTime date in block.OrderBy(d => d)) { 
     Console.WriteLine(date.ToString(" dddd dd/MM yyyy", southAfricanCuture)); 
     Console.WriteLine(date.ToString(" ddd d/M yy", southAfricanCuture)); 
     Console.WriteLine(date.ToString(" dd/MM/yy", southAfricanCuture)); 
    } 
} 
Console.ReadKey(); 

正如你可以看到,隨着d工作ates是一件複雜的事情。

+0

謝謝,這是我一直在尋找,所以基本上我應該使用date.ToString我輸出 – Brian02 2014-09-04 11:15:53

1

有這qwestion.I一個LINQ標籤試圖做的是,在LINQ

創建值列表的日期時間

var lst = new List<string> {"02/10/1998", 
    "03/10/1998", 
    "07/10/1998", 
    "10/10/1998", 
    "17/10/1998", 
    "20/10/1998" 
    }; 

值列表

var DateList = (from value in lst 
        select DateTime.ParseExact(value, "dd/MM/yyyy", 
         CultureInfo.InvariantCulture)).ToList(); 

讓我們盡我們的列表更可用

List<MyClass> lst3 = DateList.Select(p => new MyClass 
     { 
      DT = p, 
      WeekOfYear = CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(p , DateTimeFormatInfo.CurrentInfo.CalendarWeekRule, DayOfWeek.Monday), 
      FirstDayOfWeek = p.AddDays(DayOfWeek.Monday - p.DayOfWeek), 
      LastDayOfWeek = p.AddDays(DayOfWeek.Sunday - p.DayOfWeek) 
     }).OrderBy(x=>x.WeekOfYear).ToList(); 

現在你可以展示結果

int current_week=-1; 
     foreach (MyClass o in lst3) 
     { 
      if (current_week > o.WeekOfYear) 
      { 
       current_week = o.WeekOfYear; 
       Console.WriteLine("Week: {0} {1} to {2} {3}", o.FirstDayOfWeek.DayOfWeek.ToString() ,o.FirstDayOfWeek, o.LastDayOfWeek.DayOfWeek.ToString(), o.LastDayOfWeek); 
      } 
      Console.WriteLine("{0} {1}", o.DT.DayOfWeek.ToString(), o.DT); 
     } 
+0

這應該工作,在我張貼了這個問題,我是一個初學者的時間當談到LINQ時。 – Brian02 2015-07-03 09:11:28

+0

@ Brian02你是什麼意思?這個答案對你有幫助嗎? – 2015-07-17 14:02:21

+0

是的,它確實有效。謝謝 – Brian02 2015-07-20 09:48:24

相關問題