?
Solved

Update Datatable with the results of a LINQ query

Posted on 2009-06-29
4
Medium Priority
?
1,437 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 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 2000 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

752 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