平均表中的列 - 忽略某些列

时间:2011-02-21 10:31:41

标签: asp.net vb.net average

我有以下代码成功地对表中的所有列进行了平均。我需要做的是忽略这个等式中的某些列。

Dim totalNumber as Double = 0
Dim count as Integer = 0

For x = 0 To xyz123.Tables(0).Columns.Count - 1
  Dim current as Double = 0
  If Double.TryParse(xyz123.Tables(0).Rows(0)(x).ToString(), current) AndAlso current <> 0 Then
    count += 1
    totalNumber += current
  End If
Next

Dim averageRating as Double = totalNumber / count

2 个答案:

答案 0 :(得分:1)

使用您的源代码,您可以试试这个

Dim totalNumber as Double = 0
Dim count as Integer = 0

For x = 0 To xyz123.Tables(0).Columns.Count - 1

Dim current as Double = 0

If Double.TryParse(xyz123.Tables(0).Rows(0)(x).ToString(), current) AndAlso current <> 0 AndAlso x <> 13 AndAlso x <> 1 Then
    count += 1
    totalNumber += current
End If
Next

Dim averageRating as Double = totalNumber / count

在上面的示例中,这将忽略平均值

中的第14列和第2列

希望这有帮助

答案 1 :(得分:0)

如果您想按名称排除某些列,例如名为“weather”的列然后在代码中测试列(x).ColumnName

Dim totalNumber as Double = 0
Dim count as Integer = 0

For x = 0 To xyz123.Tables(0).Columns.Count - 1

    If Not xyz123.Tables(0).Columns(x).ColumnName="weather" Then
          Dim current as Double = 0
          'etc