Search code examples
c#collectionsstreamreaderopenfiledialog

How to retrieve an element from collection based on other element?


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,


Solution

  • 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;