Home > Enterprise >  Get double value using LINQ
Get double value using LINQ

Time:09-20

My program reads data (date, time, temperature value) from a CSV file and indicates the stored times after selecting a date in a list box. When clicking on a time of the list box, the associated temperature value should be displayed.

The CSV file is set up as follows:

date, time, temperature value

Example of the CSV-File:

date        time    temperature value
18.09.2022  19:05   28,33
18.09.2022  20:05   29,33
18.09.2022  21:05   30,33
18.09.2022  22:05   31,33
18.09.2022  23:05   32,33

So my program insert the times 19:05, 20:05, 21:05, 22:05, 23:05 into a listbox.

For example, if I click 21:05, the temperature value should be output 30.33.

Here's how I read the CSV file:

        private List<werte> value_liste; 
        //...

        private void read_values(string path)
        {
            using (TextFieldParser csvParser = new TextFieldParser(path))
            {                
                csvParser.CommentTokens = new string[] { "#" };
                csvParser.SetDelimiters(new string[] { "," });
                csvParser.HasFieldsEnclosedInQuotes = true;

                // Skip the row with the column names
                csvParser.ReadLine();

                while (!csvParser.EndOfData)
                {
                    fields = csvParser.ReadFields();                 
                    value_list.Add(new value { date = fields[0], time = fields[1],
                        temperature = Convert.ToDouble(fields[2]) });
                }
            }
        }

        public class values
        {
            public string date { get; set; }
            public string time { get; set; }
            public double temperatur { get; set; }
        }

private void time_listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string listbox_text = time_listbox.SelectedItem.ToString();
            
    // Here I want to get the corresponding temperature value for
    // the clicked time listbox item
}

How to get the corresponding temperature value?

CodePudding user response:

You can use .SelectedIndex property and get the temprature from your value_liste

private void time_listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string listbox_text = time_listbox.SelectedItem.ToString();
    var index = time_listbox.SelectedIndex;
    var temperatur = value_liste[index].temperatur;
}

CodePudding user response:

Cast time_listbox.SelectedItem your type and access the property directly:

private void time_listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    values selectedItem = time_listbox.SelectedItem as values;
    if (values != null)
    {
        double temp = values.temperatur;
    }
}

This assumes you have set the ItemsSource of the ListBox to an IEnumerable<values>.

Or use the DLR:

private void time_listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    dynamic selectedItem = time_listbox.SelectedItem;
    double temp = values.temperatur;
}
  • Related