Solved

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

Posted on 2015-02-13
4
218 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to Popup 2 48
Chat Room 1 22
Setting runtime form location 4 18
Format column on datatable 7 14
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

9 Experts available now in Live!

Get 1:1 Help Now