首页 > 解决方案 > 在 VB.Net 控制台中计算数组内容的数量

问题描述

我在 VB.Net 中使用控制台程序

我有数据:数据

如何创建如果我选择 HONDA 结果是 3670。 AND OR

如果我选择 APR,则结果为 1750,数据平均值为 350

请任何人帮助我。

标签: vb.netconsole-application

解决方案


您在标题中提到了数组,但我个人不会为此使用数组。我会告诉你为什么。请参阅以下示例并阅读其中的注释。

选项 1. 多维数组

Dim data As Integer(,) = {
    {150, 200, 100, 300, 250, 400, 500, 450, 350, 220, 150, 600},
    {400, 500, 450, 350, 220, 150, 600, 150, 200, 100, 300, 250},
    {300, 250, 400, 500, 450, 350, 150, 200, 100, 300, 450, 400},
    {250, 220, 450, 100, 450, 400, 350, 200, 100, 100, 450, 400},
    {100, 450, 400, 500, 600, 150, 350, 220, 100, 300, 100, 250}}
Dim HONDA_sum As Integer
Dim APR_sum As Integer
Dim APR_avg As Double
For i = 0 To UBound(data, 2)
    HONDA_sum += data(1, i)
Next
For i = 0 To UBound(data, 1)
    APR_sum += data(i, 3)
    APR_avg = (APR_avg * i + data(i, 3)) / (i + 1)
Next
' For loops are used because multidimensional arrays aren't great with LINQ
' YAMAHA row is 1. You need to hard-code this or make a lookup table
' APR column is 3. You need to hard-code this or make a lookup table
' avg arithmetic is a little complicated

选项 2. 锯齿状阵列

Dim data As Integer()() = {
        New Integer() {150, 200, 100, 300, 250, 400, 500, 450, 350, 220, 150, 600},
        New Integer() {400, 500, 450, 350, 220, 150, 600, 150, 200, 100, 300, 250},
        New Integer() {300, 250, 400, 500, 450, 350, 150, 200, 100, 300, 450, 400},
        New Integer() {250, 220, 450, 100, 450, 400, 350, 200, 100, 100, 450, 400},
        New Integer() {100, 450, 400, 500, 600, 150, 350, 220, 100, 300, 100, 250}}
Dim HONDA_sum = data(1).Sum()
Dim APR_sum = data.Sum(Function(l) l(3))
Dim APR_avg = data.Average(Function(l) l(3))
' We can use LINQ but some hard-coded column numbers are used
' YAMAHA row is 1. You need to hard-code this or make a lookup table
' APR column is 3. You need to hard-code this or make a lookup table

选项 3. Dictionary(Of Dictionary)

Dim data As New Dictionary(Of String, Dictionary(Of String, Integer)) From {
    {"YAMAHA", New Dictionary(Of String, Integer) From {{"JAN", 150}, {"FEB", 200}, {"MAR", 100}, {"APR", 300}, {"MAY", 250}, {"JUN", 400}, {"JUL", 500}, {"AUG", 450}, {"SEP", 350}, {"OCT", 220}, {"NOV", 150}, {"DEC", 600}}},
    {"HONDA", New Dictionary(Of String, Integer) From {{"JAN", 400}, {"FEB", 500}, {"MAR", 450}, {"APR", 350}, {"MAY", 220}, {"JUN", 150}, {"JUL", 600}, {"AUG", 150}, {"SEP", 200}, {"OCT", 100}, {"NOV", 300}, {"DEC", 250}}},
    {"SUZUKI", New Dictionary(Of String, Integer) From {{"JAN", 300}, {"FEB", 250}, {"MAR", 400}, {"APR", 500}, {"MAY", 450}, {"JUN", 350}, {"JUL", 150}, {"AUG", 200}, {"SEP", 100}, {"OCT", 300}, {"NOV", 450}, {"DEC", 400}}},
    {"KAWASAKI", New Dictionary(Of String, Integer) From {{"JAN", 250}, {"FEB", 220}, {"MAR", 450}, {"APR", 100}, {"MAY", 450}, {"JUN", 400}, {"JUL", 350}, {"AUG", 200}, {"SEP", 100}, {"OCT", 100}, {"NOV", 450}, {"DEC", 400}}},
    {"MINERVA", New Dictionary(Of String, Integer) From {{"JAN", 100}, {"FEB", 450}, {"MAR", 400}, {"APR", 500}, {"MAY", 600}, {"JUN", 150}, {"JUL", 350}, {"AUG", 220}, {"SEP", 100}, {"OCT", 300}, {"NOV", 100}, {"DEC", 250}}}
}
Dim HONDA_sum = data("HONDA").Sum(Function(kvp) kvp.Value)
Dim APR_sum = data.Values.Select(Function(d) d("APR")).Sum()
Dim APR_avg = data.Values.Select(Function(d) d("APR")).Average()
' A little better. Again we can use LINQ and the data can be addressed with manufacturer or month names
' but it's still clumsy to define your data in code like this

选项 3.5 列表

未实现,因为它类似于锯齿状数组,无论如何我更喜欢字典。

选项 4. 列表(元组)

Dim data As New List(Of (Manufacturer As String, Month As String, Total As Integer)) From {
    ("YAMAHA", "JAN", 150), ("YAMAHA", "FEB", 200), ("YAMAHA", "MAR", 100), ("YAMAHA", "APR", 300), ("YAMAHA", "MAY", 250), ("YAMAHA", "JUN", 400), ("YAMAHA", "JUL", 500), ("YAMAHA", "AUG", 450), ("YAMAHA", "SEP", 350), ("YAMAHA", "OCT", 220), ("YAMAHA", "NOV", 150), ("YAMAHA", "DEC", 600),
    ("HONDA", "JAN", 400), ("HONDA", "FEB", 500), ("HONDA", "MAR", 450), ("HONDA", "APR", 350), ("HONDA", "MAY", 220), ("HONDA", "JUN", 150), ("HONDA", "JUL", 600), ("HONDA", "AUG", 150), ("HONDA", "SEP", 200), ("HONDA", "OCT", 100), ("HONDA", "NOV", 300), ("HONDA", "DEC", 250),
    ("SUZUKI", "JAN", 300), ("SUZUKI", "FEB", 250), ("SUZUKI", "MAR", 400), ("SUZUKI", "APR", 500), ("SUZUKI", "MAY", 450), ("SUZUKI", "JUN", 350), ("SUZUKI", "JUL", 150), ("SUZUKI", "AUG", 200), ("SUZUKI", "SEP", 100), ("SUZUKI", "OCT", 300), ("SUZUKI", "NOV", 450), ("SUZUKI", "DEC", 400),
    ("KAWASAKI", "JAN", 250), ("KAWASAKI", "FEB", 220), ("KAWASAKI", "MAR", 450), ("KAWASAKI", "APR", 100), ("KAWASAKI", "MAY", 450), ("KAWASAKI", "JUN", 400), ("KAWASAKI", "JUL", 350), ("KAWASAKI", "AUG", 200), ("KAWASAKI", "SEP", 100), ("KAWASAKI", "OCT", 100), ("KAWASAKI", "NOV", 450), ("KAWASAKI", "DEC", 400),
    ("MINERVA", "JAN", 100), ("MINERVA", "FEB", 450), ("MINERVA", "MAR", 400), ("MINERVA", "APR", 500), ("MINERVA", "MAY", 600), ("MINERVA", "JUN", 150), ("MINERVA", "JUL", 350), ("MINERVA", "AUG", 220), ("MINERVA", "SEP", 100), ("MINERVA", "OCT", 300), ("MINERVA", "NOV", 100), ("MINERVA", "DEC", 250)}
Dim HONDA_sum = data.Where(Function(t) t.Manufacturer = "HONDA").Sum(Function(t) t.Total)
Dim APR_sum = data.Where(Function(t) t.Month = "APR").Sum(Function(t) t.Total)
Dim APR_avg = data.Where(Function(t) t.Month = "APR").Average(Function(t) t.Total)
' Another method which allows for LINQ. The queries are nice and clean. This method feels like querying SQL...
' but it's a nightmare to maintain this data import similar to the previous methods

选项 5.ORM

Using dc As New DataContext()
    Dim HONDA_sum = (From d In dc.Datas Where d.Manufacturer = "HONDA" Select d.Total).Sum()
    Dim APR_sum = (From d In dc.Datas Where d.Month = "APR" Select d.Total).Sum()
    Dim APR_avg = (From d In dc.Datas Where d.Month = "APR" Select d.Total).Average()
End Using
' This is hypothetical, but assuming your data is in a database table with columns
' Table: Data
' Columns: Month (string), Manufacturer (string), Total (integer)
' and you have used EF or LinqToSQL to generate an ORM
' this allows a very clean implementation using a standard language i.e. LINQ / SQL
' Expression LINQ or Lambdas can be used, both resulting in the same underlying query

如果您有数据库,我推荐选项 5。这也可以使用 Excel 甚至文本文件之类的东西来完成,但它的好处是一旦您设置了实体映射(最好使用 IDE 工具......),相同的代码可以应用于您的任何数据源。

除了不适用的选项 5 之外,您可能会注意到,随着数据导入变得更加复杂,数据检索变得更加简单。这是您在开发应用程序之前需要考虑的权衡


推荐阅读