Solved

Update Datatable with the results of a LINQ query

Posted on 2009-06-29
4
1,425 Views
Last Modified: 2013-11-11
Not sure how/or best way to do this. I have a datatable with part of the data needed (look at code). The code works. But now I want to run another LINQ query something like this

Dim TList = From TL In dc.vwTL
          Select TL.DOD, TL.Test

and now update my Datatable with the TL.Test info.  I actually have many other columns that will need to update the datatable the same way to include calculations.  Using VB 2008 framework 3.5.  had a simular question. Thanks in advance
Dim TMDODList = From TM In dc.vwCurrent_datTMs _

                        Select TM.DOD
 

            ResultsTable = New DataTable("Results")
 

              'Create the cols for the table

            ResultsTable.Columns.Add("DOD", GetType(String))

            ResultsTable.Columns.Add("Test", GetType(Integer))
 

            For Each DOD In TMDODList

                ResultsTable.Rows.Add(New Object() {DOD})

            Next

Open in new window

0
Comment
Question by:MadIce
  • 2
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 24761476
I am not sure if i understand your question correctly but if you need to update a column value in the datatable then something like following code could be used.

This is a demo code.
for each T in TList

    ResultsTable.DefaultView.RowFilter = "Test=" & T.TestID

    If ResultsTable.DefaultView.Count = 1 Then

       ResultsTable.DefaultView.Item(0).Item("DOD") = TList.DOD

    End If

    ResultsTable.DefaultView.RowFilter = ""

Next

Open in new window

0
 

Author Comment

by:MadIce
ID: 24764376
CodeCruiser,
It looks like your example will work but I get an error on the the second line (for the Rowfilter).
The error is "Cannot find Column(A1).
A1 is the value for the first DOD. I need to filter on DOD not Test. It seem like that should work. Any Ideas
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 500 total points
ID: 24764431
In the RowFilter, you specify the column caption not value. If its varchar type then try this


ResultsTable.DefaultView.RowFilter = "DOD='" & T.DOD & "'"

Open in new window

0
 

Author Closing Comment

by:MadIce
ID: 31598100
I had to adjust code for nullable value but answer is just what I was looking for.
If T.Test.HasValue Then ResultsTable.DefaultView.Item(0).Item("Test") = T.test
Thanks alot. Was getting ready to go back to using Store Procedure
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now