0

I'm reading a number of txt files. I'm reading each line, splitting and creating an object by finally adding to collection.

SAMPLE FILE CONTENT:

Record1,Time,Celsius(°C),High Alarm,Low Alarm,Humidity(%rh),dew point(°C),Serial Number
1,02/06/2017 09:26:30,19.5,32.0,7.0,64.0,12.5,11211222
2,02/06/2017 09:31:30,21.0,32.0,7.0,54.5,11.4
3,02/06/2017 09:36:30,20.5,32.0,7.0,54.5,11.0

List<MagModel> Records = new List<MagModel>();

using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Text 
File|*.txt", Multiselect = true })
{
    ofd.InitialDirectory = HelperClass.GetDirectory;

    if (ofd.ShowDialog() == DialogResult.OK)
    {
        foreach (string file in ofd.FileNames)
        {
            using (StreamReader reader = new StreamReader(file))
            {
                reader.ReadLine();
                if (file.Contains("Record1"))
                {
                    var lines = from line in reader.Lines()
                                where !string.IsNullOrEmpty(line)
                                select line;

                    foreach (var line in lines)
                    {
                        var result = line.Split(',');

                        MagModel model1 = new MagModel();

                        model1.magazine = "Record1";                                   
                        model1.date = result[1];
                        model1.temp = Convert.ToDouble(result[2]);
                        model1.humidity = Convert.ToDouble(result[5]);
                        Records.Add(model1);

                    }

                    MaxMinTemperature.maxTemp = Records.Max(r => r.temp);
                    MaxMinTemperature.minTemp = Records.Min(r => r.temp);
                }
            }
        }
    }
}

Then I`m finding the highest and lowest temperatures within collection (21.0 and 19.5 in the example):

MaxMinTemperature.maxTemp = Records.Max(r => r.temp);
MaxMinTemperature.minTemp = Records.Min(r => r.temp);

How do I find the date where the highest or/and lowest temperature was collected ?

Thank you,

Aleks Andreev
  • 6,732
  • 8
  • 27
  • 36
Thomas
  • 67
  • 1
  • 11
  • 2
    Please do not mix content parsing and data analyzing – Aleks Andreev Aug 05 '17 at 13:49
  • Possible duplicates: [here](https://stackoverflow.com/questions/914109/how-to-use-linq-to-select-object-with-minimum-or-maximum-property-value), [here](https://stackoverflow.com/questions/1101841/linq-how-to-perform-max-on-a-property-of-all-objects-in-a-collection-and-ret). Essentially, you need to retrieve the object associated with the max/min value, and use the properties as you need. – crazyGamer Aug 05 '17 at 14:08

1 Answers1

0

Define your model like this:

public class MagModel
{
    public int Record1 { get; set; }
    public DateTime Time { get; set; }
    public double Celsius { get; set; }
    public double HighAlarm { get; set; }
    public double LowAlarm { get; set; }
    public double Humidity { get; set; }
    public double DewPoint { get; set; }
    public string SerialNumber { get; set; }
}

then write separate parsing method:

public static IEnumerable<MagModel> ParseFile(string filename)
{
    // put your parsing here
}

now it will be easy to get dates of max and min temperatures:

var input = ParseFile("input.csv");
var orderByCelsius = input.OrderBy(m => m.Celsius).ToArray();
var minTemperatureDate = orderByCelsius.FirstOrDefault()?.Time;
var maxTemperatureDate = orderByCelsius.LastOrDefault()?.Time;
Aleks Andreev
  • 6,732
  • 8
  • 27
  • 36