Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

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.
0
g_johnson
Asked:
g_johnson
  • 2
1 Solution
 
AndyAinscowCommented:
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
0
 
Fernando SotoCommented:
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

0
 
Fernando SotoCommented:
Hi g_johnson;

Here is a code snippet that will work with Linq to SQL. In order to use this code you will need to set your DBML to enable serialization. To do this open your DBML in design mode, right click on the design surface where no objects are and select properties. In the properties window change Serialization Mode to Unidirectional.

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 = Clone(hdr);
// 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;
//....

// The following method gets implemented as is with no changes
// Gotten from the Web : LINQ to SQL tips and tricks #2
// http://damieng.com/blog/2009/04/12/linq-to-sql-tips-and-tricks-2
public static T Clone<T>(T source)
{
    var dcs = new DataContractSerializer(typeof(T));
    using (var ms = new System.IO.MemoryStream())
    {
        dcs.WriteObject(ms, source);
        ms.Seek(0, System.IO.SeekOrigin.Begin);
        return (T)dcs.ReadObject(ms);
    }
}

Open in new window

0
 
g_johnsonAuthor Commented:
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!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now