如何在循环浏览列表时比较日期时间对象
本文关键字:比较 日期 时间 对象 列表 循环 浏览 | 更新日期: 2023-09-27 18:36:30
我正在尝试循环浏览包含两个字段的列表(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());
}
}
}
}
}
}
}
任何帮助表示赞赏。谢谢。
合
二为一,在日期时间上使用 Max() 函数,而不是自己进行测试。
var result = indexList
//"transform" your initial list of strings into an IEnumerable of splitted strings (string[])
.Select(list => list.Split(','))
//in this new List of string[], select the first part in text, select and Convert the second part in DateTime.
//We now have an IEnumerable of anonymous objects, composed of a string and a DateTime Property
.Select(splittedList => new
{
text = splittedList[0],
date = new DateTime(Convert.ToInt32(splittedList[1].Substring(4, 4)),
Convert.ToInt32(splittedList[1].Substring(2, 2)),
Convert.ToInt32(splittedList[1].Substring(0, 2)))
})
//group that new List by the text Property (one "entry" for each distinct "text").
//GroupBy creates an IGrouping<out TKey, out TElement>, kind of special dictionary, with an IEnumerable<TResult> as "value" part
//(here an IEnumerable of our anonymous object)
.GroupBy(textDateTimeList => textDateTimeList.text)
//from this grouping, take the "key" (which is the "distinct text", and in the IEnumerable<anonymousObject>, take the Max Date.
//We now have a new List of anonymous object, with a string Property and a DateTime Property
.Select(group => new
{
stringField = group.Key,
maxDateField = group.Max(dateField => dateField.date)
});