Fastest, Easiest, Most Efficient way to duplicate a Datatable column?

Posted on 2012-09-21
Last Modified: 2012-09-24
Hi Experts,

Quick question - Hopefully I don't know it, but what is the fastest way to duplicate a column in a datatable with all the data?

In other words, suppose I have a datatable with a column called "col1" populated with values. What I would like is to add a new column to my datatable called, say, col1_copy and have it have all the exact same values as col1.

I'm hoping there is a faster way than just:
Add column to Datatble with same structure as col1
Loop through datarows and copy from one column to the other
since my datatables are ENORMOUS and I'm duplicating quite a few columns and, so, updating the datatable value-by-value seems to be very slow...

PLEASE let me know the fastest, most efficient way to do this???

Question by:origfla
    LVL 25

    Accepted Solution

    How do you fill originally the data in your "col1" column? Because the fastest way would be to fill the col1_copy column from the origin of data. I mean, if you have a SELECT returning the data to your datatable, add a new column to the SELECT with the AS col1_copy clause. This would be the fastest way.

    If you want to do it exclusively into .Net code, then a loop will be mandatory. You can use LINQ to do the stuff, be although the syntax will be LINQ and probably you can do it in only 1 line of code, internally it will execute a Loop thru all values.

    Hope that helps.
    LVL 83

    Expert Comment

    If the above comment does not apply then I think looping and copying is the only option( I am not sure if LINQ can help here)

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
    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…
    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.…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    754 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

    22 Experts available now in Live!

    Get 1:1 Help Now