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
Solved

Update Datatable with the results of a LINQ query

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

840 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