2 つのフィールドを含むリスト (csv) をループしようとしています。名前と日付。リストにはさまざまな重複した名前とさまざまな日付があります。リスト内の名前ごとに、同じ名前のインスタンスが複数あり、対応する日付が最新であると推測しようとしています。
別の回答を見て、DateTime.Compare メソッドを使用する必要があることに気付きましたが、問題はどの日付が遅いかを判断することです。これがわかったら、一意の名前とそれに関連する最新の日付を含むファイルを作成する必要があります。
これは私を初心者にする最初の質問です。
編集:
最初は、LatestDate オブジェクトをファイルに表示されない日付に設定しても問題ないと思っていたので、ファイル内のそれ以降の日付を LatestDate にしました。
これまでの私のコーディングは次のとおりです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace flybe_overwriter
{
class Program
{
static DateTime currentDate;
static DateTime latestDate = new DateTime(1000,1,1);
static HashSet<string> uniqueNames = new HashSet<string>();
static string indexpath = @"e:\flybe test\indexing.csv";
static string[] indexlist = File.ReadAllLines(indexpath);
static StreamWriter outputfile = new StreamWriter(@"e:\flybe test\match.csv");
static void Main(string[] args)
{
foreach (string entry in indexlist)
{
uniqueNames.Add(entry.Split(',')[0]);
}
HashSet<string>.Enumerator fenum = new HashSet<string>.Enumerator();
fenum = uniqueNames.GetEnumerator();
while (fenum.MoveNext())
{
string currentName = fenum.Current;
foreach (string line in indexlist)
{
currentDate = new DateTime(Convert.ToInt32(line.Split(',')[1].Substring(4, 4)),
Convert.ToInt32(line.Split(',')[1].Substring(2, 2)),
Convert.ToInt32(line.Split(',')[1].Substring(0, 2)));
if (currentName == line.Split(',')[0])
{
if(DateTime.Compare(latestDate.Date, currentDate.Date) < 1)
{
// Console.WriteLine(currentName + " " + latestDate.ToShortDateString() + " is earlier than " + currentDate.ToShortDateString());
}
else if (DateTime.Compare(latestDate.Date, currentDate.Date) > 1)
{
// Console.WriteLine(currentName + " " + latestDate.ToShortDateString() + " is later than " + currentDate.ToShortDateString());
}
else if (DateTime.Compare(latestDate.Date, currentDate.Date) == 0)
{
// Console.WriteLine(currentName + " " + latestDate.ToShortDateString() + " is the same as " + currentDate.ToShortDateString());
}
}
}
}
}
}
}
どんな助けでも感謝します。ありがとう。