简体   繁体   English

在.csv 文件中找到最高数并显示

[英]Find highest number in .csv file and display it

What can I do that this Programm will output the highest number of my csv file?我该怎么办这个程序将 output 我的 csv 文件的最高编号? Currently it displays the value in the in the Brackets of the .Content line目前它在.Content行的括号中显示值

private void BtnAuswerten_Click(object sender, RoutedEventArgs e)
{
    using (var reader = new StreamReader("H:/Projekte/MaxTemp2/MaxTemp/MaxTemp/temps.csv"))
    {
        List<string> listA = new List<string>();
        List<string> listB = new List<string>();
        List<string> listC = new List<string>();

        while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            var values = line.Split(',');

            listA.Add(values[0]);
            listB.Add(values[1]);
            listC.Add(values[2]);
        }

        lblAusgabe.Content = (string.Join(",", listA[5], listB[5], listC[5]));
    }
}

You can use a DataTable instead of three independent lists:您可以使用 DataTable 而不是三个独立的列表:

[...] [...]

    DataTable dtValues = new DataTable();
    dtValues.Columns.Add("Column1", typeof(int));
    dtValues.Columns.Add("Column2", typeof(string));
    dtValues.Columns.Add("Column3", typeof(string));
    
    while (!reader.EndOfStream)
    {
        var line = reader.ReadLine();
        var values = line.Split(',');

        dtValues.Rows.Add(values[0],values[1],values[1]);
    }
    
    dtValues.DefaultView.Sort = "Column1 DESC";
    dtValues = dtValues.DefaultView.ToTable();

[...] [...]

That snippet assumes that the first column contains the value you want to sort.该片段假定第一列包含您要排序的值。 But I'm sure you can adapt it for your needs.但我相信你可以根据自己的需要调整它。

When you try to print your highest value on lblAusgabe , do it like that:当您尝试在lblAusgabe上打印最高值时,请这样做:

lblAusgabe.Content = (string.Join(",", dtValues.Rows[0].Field<int>("Column1"), dtValues.Rows[0].Field<string>("Column2"), dtValues.Rows[0].Field<string>("Column3")));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM