使用 Linq 从 VB.NET / C# Winforms 应用程序中的数据表列中获取值

问题描述 投票:0回答:0

我有一个项目及其具体相关数据的数据表,如成本、税务信息数量等。 此数据表中的一列是发票编号。对于一个这样的集合中存在的所有不同项目,重复相同的发票编号值。 我在一个数据表中有 100 个这样的集合。

我需要计算的是每个发票号的“项目列成本”和“项目总税额”列的总和,并在同一表中更新属于同一发票号的所有行.我将在下面以表格的形式举例说明。

基本上我已经能够通过使用 for 循环和数据视图等来完成此操作,但是通过执行所有这些操作,我的代码会多次遍历数据表,这对性能影响非常大。

我希望你们中的某个人能够帮助我处理 Linq 查询,我可以在数据表上触发并一次性获取值,并在一个 For 循环中为所有行更新相同的值。

任何其他建议也将不胜感激。

请不要问我,为什么数据是这样的,为什么我们不能改变数据结构,因为我们不能。这是一个真实世界的例子,我所需要的只是一点指导来一次性获得结果,而不是在同一个数据表中迭代多次。

问题不在于 10 到 15 行,这是可管理的,当我在数据表中有超过 130 行时,问题就来了,那时候它开始影响代码的性能。

Two Tables, Top one is Source Datatable and bottom is To be Datatable

目前我正在做的是,我首先创建一个具有唯一发票编号的临时表。 然后我循环遍历我的数据表的所有行,并从唯一发票表中找到所有项目的总计和每张发票的总税额。 然后我再次遍历原始数据表以更新原始数据表中各个发票的总项目金额和总税额。

这导致我在同一个数据表上多次循环,我也无法使用 exit for statement 因为发票号有可能出现在数据表中的任何地方,所以基本上对于唯一标识的每张发票用于计算和更新数据表中的值的发票表我被迫遍历整个数据表。

相反,我希望的是,如果有任何机制可以让我们直接找到特定发票编号的项目金额总额和税额总额,并在数据表的单个 for 循环中一次性更新它。

我可以使用 SQL 查询非常轻松地做到这一点,但不确定如何为这项工作编写 Linq 查询。 问题是这个数据表在数据库中不以这种格式存在,我通过我的代码对这个数据表执行了很多操作,这一步是在整个活动结束时进行的。

Dim dv1 As DataView = my_format_dt.DefaultView
            Dim unique_invoice_dt As DataTable = dv1.ToTable(True, "supplier_id_invoice_number_combo")
            If unique_invoice_dt IsNot Nothing AndAlso unique_invoice_dt.Rows.Count > 0 Then
                For row_cntr_unique_invoices = 0 To unique_invoice_dt.Rows.Count - 1
                    Dim unique_combo As String = unique_invoice_dt.Rows(row_cntr_unique_invoices).Item("supplier_id_invoice_number_combo").ToString
                    Dim total_item_amount As Double = 0
                    Dim total_tax_amount As Double = 0
                    For row_cntr2 = 0 To my_format_dt.Rows.Count - 1
                        Dim check_string As String = my_format_dt.Rows(row_cntr2).Item("supplier_id_invoice_number_combo").ToString
                        If check_string = unique_combo Then
                            total_item_amount = total_item_amount + Convert.ToDouble(my_format_dt.Rows(row_cntr2).Item("total_cost_price_of_item").ToString)
                            Dim cgst_percentage As Double = Convert.ToDouble(my_format_dt.Rows(row_cntr2).Item("cgst_percentage").ToString)
                            Dim sgst_percentage As Double = Convert.ToDouble(my_format_dt.Rows(row_cntr2).Item("sgst_percentage").ToString)
                            Dim cgst_amount As Double = Math.Round(((cgst_percentage / 100) * Convert.ToDouble(my_format_dt.Rows(row_cntr2).Item("total_cost_price_of_item").ToString)), 2)
                            Dim sgst_amount As Double = Math.Round(((sgst_percentage / 100) * Convert.ToDouble(my_format_dt.Rows(row_cntr2).Item("total_cost_price_of_item").ToString)), 2)
                            total_tax_amount = total_tax_amount + cgst_amount + sgst_amount
                        End If
                    Next
                    For row_cntr3 = 0 To my_format_dt.Rows.Count - 1
                        Dim check_string As String = my_format_dt.Rows(row_cntr3).Item("supplier_id_invoice_number_combo").ToString
                        If check_string = unique_combo Then
                            my_format_dt.Rows(row_cntr3).Item("invoice_item_amount") = Math.Round(total_item_amount, 2)
                            my_format_dt.Rows(row_cntr3).Item("invoice_tax_amount") = Math.Round(total_tax_amount, 2)
                        End If
                    Next
                Next
            End If
vb.net winforms linq datatable dataview
© www.soinside.com 2019 - 2024. All rights reserved.