Solved

Update Datatable with the results of a LINQ query

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

732 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