Fill a datatable from a custom object

Posted on 2012-09-20
Last Modified: 2012-09-21

I have programmed a custom object that looks something like this:

Public Class MyObject

public property prop1 as string
public property prop2 as integer
public property prop3 as double

public property dr as datarow

And I have created a list(of MyObject)

What i am looking to do is pull from this list, say:
prop1, dr.item("Item_a"), dr.item("Item_j")

(In other words a property or 2 and a few items from the datarow... Not all the items) for each object in the list and to populate that data into a datatable / new custom object (preferably datatable) so i can use it to update something else.

How can I achieve this in the best way?
I'm thinking LINQ, but it's not working for me...

Question by:origfla
    LVL 9

    Expert Comment

    What I would do is adding a method to this class which returns an array of object (containing the data for this row... 1 column / array slot).

    Then, outside of it, you can use this method to add a new row to the datatable you created.
    LVL 83

    Accepted Solution

    Something like

    Dim dTable As New DataTable
    dTable.Columns.Add("Prop1", GetType(Integer))
    dTable.Columns.Add("Item_A", GetType(String))
    dTable.Columns.Add("Item_J", GetType(String))

    For Each obj As MyObject in objList
       dTable.Rows.Add(new Object() { obj.Prop1, obj.dr("Item_A"), obj.dr("Item_J") })

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    759 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

    10 Experts available now in Live!

    Get 1:1 Help Now