Solved

Update Datatable with the results of a LINQ query

Posted on 2009-06-29
4
1,431 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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