[英]Selecting distinct DataTable rows
In my vb.net project, I have this form, which is used for adding additional charges onto orders (delivery costs, for example). 在我的vb.net项目中,我有此表单,用于将额外费用添加到订单中(例如,交付成本)。
In this example, the freight number is the same, because the freight is for the full order, rather than having freight for each line. 在此示例中,货运编号是相同的,因为货运用于全订单,而不是每行都有货运。 However, there is an option to add a different freight value to each individual order line, so the numbers would be different (eg, 7 and 8), and so would the value for each line.
但是,可以选择为每个单独的订单行添加不同的运费值,因此数量会有所不同(例如7和8),因此每个行的值也会不同。
When the Add button is pressed, either the line or the order (depending on how freight is being added), is added to the grid labelled 'Freight'. 当按下添加按钮时,行或订单(取决于货运方式)将被添加到标有“货运”的网格中。 But, the data is only stored in a
DataTable
, which is then passed into the Order Save function, from which the rows are copied into the database and then removed from the DataTable
. 但是,数据仅存储在
DataTable
,然后传递到Order Save函数中,从该函数中将行复制到数据库中,然后从DataTable
删除。
Now, notice the textbox
labelled 'Total Freight'... This is currently iterating over each row of the DataTable
and adding all of the totals together. 现在,注意标记为“总运费”的
textbox
...当前正在对DataTable
每一行进行迭代,并将所有总计加在一起。 This is fine for if the freights are all different, however, if it is added by order, and there are 2 lines, as above, the value should be 15, not 30, which is currently shown. 如果运费不同,这很好,但是,如果按订单添加运费,并且如上所述有2行,则该值应为15,而不是30(当前显示)。
So, is there a way that I can only get it to iterate over rows with different Freight # values, so that it doesn't add the same row value twice? 因此,有没有办法让我只能遍历具有不同Freight#值的行,以使它不会两次添加相同的行值? I don't think a
SQL SELECT DISTINCT
would work here, as it is only a DataTable
? 我认为
SQL SELECT DISTINCT
在这里不起作用,因为它只是一个DataTable
?
This was hard to explain, but hopefully this makes some sense, but sorry if it doesn't! 这很难解释,但希望这是有道理的,但是如果没有,抱歉!
EDIT 编辑
Code for calculating the total (freightTable is my DataTable
) 用于计算总计的代码(freightTable是我的
DataTable
)
If freightTable.Rows.Count = 0 Then
txtTotal.Text = "0.00"
Else
Dim tot As Double = 0
Dim i As Integer = 0
For i = 0 To freightTable.Rows.Count - 1
tot = tot + Convert.ToDouble(ugFreight.Rows(i).Cells("Freight_Val").Value)
Next i
txtTotal.Text = tot
End If
Keep a note of which freight values you have seem in a list. 记下您在列表中看到的运费值。
Dim freightDone As New List(Of Integer)
For i = 0 To freightTable.Rows.Count - 1
Dim currentFreigth as Integer = Convert.ToInteger( ugFreight.Rows(i).Cells("Freight_#").Value )
If Not freightDone.Contains(currentFreigth) Then
tot = tot + Convert.ToDouble(ugFreight.Rows(i).Cells("Freight_Val").Value)
freightDone.Add(currentFreigth)
End If
Next i
(You may have to change the definition of currentFreigth to fit your datatable column name, or my slightly rusty VB.) (您可能必须更改currentFreigth的定义以适合您的数据表列名称,或我的略微生锈的VB。)
You can select distinct values from a column using linq this way: 您可以使用linq这样从列中选择不同的值:
Dim Values = table.AsEnumerable().Select(Function(x) x.Field(Of int)("SomeColumn")) _
.Distinct().ToList()
在查询的where子句中添加条件:
where freight IN (select distinct freight from 'table_name')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.