Solved

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

Posted on 2015-02-13
4
219 Views
Last Modified: 2015-03-19
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
Comment
Question by:g_johnson
  • 2
4 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40607849
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
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40609360
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
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40610147
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
 
LVL 4

Author Closing Comment

by:g_johnson
ID: 40676827
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

18 Experts available now in Live!

Get 1:1 Help Now