0

我有一个 CSV 文件,每行只包含数字

例子

12,34,56
1,2,3 
34,45,67

我想得到每行的最大值、最小值和平均值。

我开始编码

from str in File.ReadLines(@"FilePath")
.Select(x => Convert.ToInt32(x))

但我不确定如何从 CSV 文件中分离值并投影每行的最大值、最小值和平均值。

如果您仍然需要更多信息,我很乐意提供。

4

1 回答 1

1

您可以使用以下代码片段

 var result = from str in File.ReadLines(@"FilePath")
                          let GetValue = str.Split(',')
                          .Select(x => Convert.ToInt32(x))
                          select new
                          {
                              Maximum = GetValue.Max(),
                              Minimum = GetValue.Min(),
                              Average = GetValue.Average()
                          };

我模拟了结果

 IEnumerable<string> lines = new[] { "1,2,3", "4,5,6", "45,56,67" };
       var result = from str in lines
                      let GetValue = str.Split(',')
                      .Select(x => Convert.ToInt32(x))
                      select new
                      {
                          Maximum = GetValue.Max(),
                          Minimum = GetValue.Min(),
                          Average = GetValue.Average()
                      };
于 2013-06-23T17:05:31.007 回答