まずあなたがを定義する必要がありますCultureInfoあなたはすべてのテキスト変換に使用します。あなたの例は米国のように見えるので、そのようなものを使うことができます。
CultureInfo culture = new CultureInfo("en-us");
次に、これらのテキスト行をDateTimesとして解析する必要があります。 個別には、重複して[並べ替えがないことを確認します曜日が指定された日付から始まる7日間反復することによって列挙することができ、デフォルトのDateTime比較(昇順)
//Use full path instead of "input.txt"
IEnumerable<DateTime> datesInFile = File.ReadAllLines(@"input.txt")
.Select(s => DateTime.Parse(s, culture))
.Distinct()
.OrderBy(d => d);
で結果をソートします。
//This date should probably come from somewhere else
DateTime startDate = new DateTime(2013, 04, 08);
IEnumerable<DateTime> datesInWeek = Enumerable.Range(0, 6)
.Select(d => startDate.Date.AddDays(d));
すべてのタイムスタンプが必要なので、日付でグループ化する必要があります。 ToDictionaryはlambdaを期待しています。最初はキー(日付)、値(その日のタイムスタンプのリスト)です。
Dictionary<DateTime, IEnumerable<DateTime>> result = datesInWeek
.ToDictionary(
d => d,
d => datesInFile.Where(dif => d.Date == dif.Date));
最後に、あなたが(もちろん、特定の文化やフォーマットで)
string outputText = result.Aggregate("",
(current, pair) => current +
pair.Key.ToString("ddd MMM d", culture) +
Environment.NewLine +
String.Join(Environment.NewLine, pair.Value.Select(
d => d.ToString("MM/dd/yyyy hh:mm:ss tt", culture))) +
Environment.NewLine);
コンパクト版あなたの結果を取得し、それらを集約することができます
CultureInfo culture = new CultureInfo("en-us");
IEnumerable<DateTime> datesInFile = File
.ReadAllLines(@"C:\Temp\input.txt")
.Select(s => DateTime.Parse(s, culture))
.Distinct()
.OrderBy(d => d);
string outputText = Enumerable
.Range(0, 6)
.Select(d => new DateTime(2013,04,08).Date.AddDays(d))
.ToDictionary(d => d, d => datesInFile.Where(dif => d.Date == dif.Date))
.Aggregate("", (current, pair) => current + pair.Key.ToString("ddd MMM d", culture) + Environment.NewLine +
String.Join(Environment.NewLine, pair.Value.Select(d => d.ToString("MM/dd/yyyy hh:mm:ss tt", culture))) +
Environment.NewLine);
を