Link to home
Start Free TrialLog in
Avatar of g_johnson
g_johnsonFlag for United States of America

asked on

Using linq to sql can I easily copy a large record in c#

Given this (this being in a loop where "header" is a different linq object):

                            hdrTable hdr = (from hh
                                                in dc.hdrTable_sqls
                                                where hh.ord_no == header.po_no
                                                orderby hh.ID descending
                                                select hh).FirstOrDefault();

                            hdrTable_sql newhdr = new hdrTable_sql();

                            newhdr.rcv_exch_rt_fg = null;
                            newhdr.inv_exch_rt_fg = null;
                            newhdr.updt_in_progress = null;
                            newhdr.hst_dt = DateTime.Now.Date;
                            newhdr.hst_tm = captureTime;

                            //TODO: set the rest of the fields

Is there a way to set the values of all the columns in hdrTable to be equal to the values of the corresponding columns in hdr without actually entering hdrTable.col1 = hdr.col1, etc.?

The table has 150 columns, and I am trying to find a shortcut.
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

You could loop through the fields collection
x.Fields(i) = y.Fields(i)
BUT they must have the same number of fields and in the same order
Hi g_johnson;

THIS ONLY WORKS WITH Linq To Entity Framework.

The following code snippet is the simplest way to do what you need to do.

// you create your data context
var dc = new ....;

// Turn off proxy creation so that it does not interfere
// when adding the new entity to the data context
dc.Configuration.ProxyCreationEnabled = false;

hdrTable hdr = ( from hh in dc.hdrTable_sqls
                 where hh.ord_no == header.po_no
                 orderby hh.ID descending
                 select hh).FirstOrDefault();

// Create the copy of the record.
hdrTable newhdr = hdr.ShallowCopy();
// Replace TABLEID with the Primary key name oand 
// setting it to zero
newhdr.TABLEID = 0;
newhdr.rcv_exch_rt_fg = null;
newhdr.inv_exch_rt_fg = null;
newhdr.updt_in_progress = null;
newhdr.hst_dt = DateTime.Now.Date;
newhdr.hst_tm = captureTime;

// We create a new patial class for the hdrTable
// to add a ShallowCopy method. We add it here
// and not in the original class so that if you 
// regenerate the model it will NOT over write this method
using System;

namespace "Replace With The Namespace of the table with the same name"
{
    public partial class hdrTable
    {
        public hdrTable ShallowCopy()
        {
            return (hdrTable)this.MemberwiseClone();
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of g_johnson

ASKER

Hi.  I was just in the last couple of days able to get back to this project and test this.  Thank you for the help!