I have a class that is like this:
class DateTimeProperty
{
public string Id { get; set; }
public DateTime TDateTime { get; set; }
}
and I use it like so:
List<DateTimeProperty> dtp = new List<DateTimeProperty>();
and I fill the list like so:
var item = new DateTimeProperty
{
Id = id,
TDateTime = transactioDateTime
};
dtp.Add(item);
Then I get the maximum date like so
maxDate = dtp.Max(x => x.TDateTime);
but what I need is the Id that has that date. Can you please show me how to get this? The whole code is below:
static void Main(string[] args)
{
List<DateTimeProperty> dtp = new List<DateTimeProperty>();
var format = "dd/MM/yyyy HH:mm:ss.fff";
var folderPath = @"I:\LCS Transactions";
DirectoryInfo di = new DirectoryInfo(folderPath);
List<DateTime> dateTimeArray = new List<DateTime>();
DateTime maxDate;
FileInfo[] fi = di.GetFiles();
foreach(FileInfo f in fi)
{
var file = GetHeader(f.FullName);
if (!file.Contains(".txt"))
{
if (file.Contains("_"))
{
var spike = file.Split('_');
var transactionType = spike[0];
var dateTime = spike[3];
var id = spike[4];
var year = dateTime.Substring(0, 4);
var month = dateTime.Substring(4, 2);
var day = dateTime.Substring(6,2);
var hour = dateTime.Substring(8, 2);
var minute = dateTime.Substring(10, 2);
var seconds = dateTime.Substring(12, 2);
var miiliseconds = dateTime.Substring(14, 3);
var stringDate = $@"{day}/{month}/{year} {hour}:{minute}:{seconds}.{miiliseconds}";
DateTime transactioDateTime = DateTime.ParseExact(stringDate, format, CultureInfo.InvariantCulture);
var item = new DateTimeProperty
{
Id = id,
TDateTime = transactioDateTime
};
Console.WriteLine(transactioDateTime);
//dateTimeArray.Add(transactioDateTime);
dtp.Add(item);
}
}
}
maxDate = dtp.Max(x => x.TDateTime);
Console.WriteLine("=====================");
Console.WriteLine(maxDate);
Console.ReadLine();
}