Solved

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

Posted on 2015-02-13
4
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 63

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 63

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

626 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