Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27069

VS 2010 [RESOLVED] Linq updating datatable weird

$
0
0
Hello

I have a .net page where I am pulling a report from the database, altering the data slightly, and then emailing it out.

The report from the database takes a bit to run, so I built the page to pull all the data in one go, then use Linq to seperate out the information I need.

This was working fine until I got the requirement to alter some of the data. The way the database is setup its more efficient to alter the columns at the application side using Linq.

What is occuring for me is that in the below code the GenerateReport for Bob runs correct, the cars values are 0 and the boats values are left un altered. However when Ralphs report runs the cars columns are already 0 before it gets to the ForEach statement. Reversing the order the reports are called causes the data to be incorrect for Bob instead of Ralph.

I checked in the debugger and it looks like when the ForEach loop runs the update is being applied to the variables detailLocal and _data. I want _data to be untouched after LoadData() is called. Calling LoadData() inside GenerateReport resolves the issue but slows down the report.

I am assuming the issue is because when I assign _data to detailLocal it is just making detailLocal point to _data. What is the correct way to assign _data to detailLocal so that I can update detailLocal without updating _data?

Code:

    Private _data As New DataTable

    Protected Sub SubmitBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitBtn.Click
        LoadData()
       
        GenerateReport("AugustSales", "Bob")
        GenerateReport("AugustSales", "Ralph")

    End Sub

    Private Sub LoadData()
        'sql command to load data...
        da.Fill(_data)
    End Sub

    Private Sub GenerateReport(Byval SalesType as String, Byval SalesPerson as String)   

    Dim detailLocal As New DataTable

    detailLocal = _data

    Dim results As EnumerableRowCollection(Of DataRow)

    results = From s In detailLocal.AsEnumerable() _
                  Where s("Month") = SalesType _
                Select s

    If SalesPerson = "Bob" Then
        Dim sales = From a in results Where a("Person") = SalesPerson Select a
        Array.ForEach(sales.ToArray, Sub(row) row("cars") = 0)
    Else
        Dim sales = From a in results Where a("Person") = SalesPerson Select a
        Array.ForEach(sales.ToArray, Sub(row) row("boats") = 0)
    End If

    SendEmail(results.CopyToDataTable)

    End Sub

    Private Sub SendEmail(Byval data as DataTable)
        'Email Report
    End Sub


Viewing all articles
Browse latest Browse all 27069

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>