473,387 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Copy Linq objects

Hello,

I'm using LINQ to access a SQL Server database. The user needs to be able to
duplicate a record. Is there an easy way to do this? I rather not have to set
each property from one to the other. I need to copy all the relationships
too.

Thanks for any help, I really appreciate it.

Thanks,
Nick
Jun 27 '08 #1
4 4438
I don't think there is anything built in; however, you can probably
automate much of the work - the following makes a fairly crude (but very
quick) shallow-copy using the default .ctor() [making a point of
ignoring the primary key field(s) - although it isn't really
LINQ-specific] - with the advantage that since this is an extension
method, you could replace on an individual basis by adding a specific
Clone() method to the partial class, and it will be
chosen by the compiler.

using System;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Linq.Expressions;
static class Program
{
static void Main()
{
Foo foo = new Foo { Id = 16, Name = "Fred", DoB = DateTime.Today };
Foo bar = foo.Clone();
}
}
class Foo
{
[Column(IsPrimaryKey = true)] // PK
public int Id { get; set; }
[Column] // test with non-PK ColumnAttribute
public string Name { get; set; }
// test w/o ColumnAttribute
public DateTime DoB { get; set; }
}
public static class ObjectExt
{
public static T Clone<T>(this T obj) where T : new()
{
return ObjectExtCache<T>.Clone(obj);
}
static class ObjectExtCache<Twhere T : new()
{
private static readonly Func<T, Tcloner;
static ObjectExtCache()
{
ParameterExpression param = Expression.Parameter(typeof(T),
"in");

var bindings = from prop in typeof(T).GetProperties()
where prop.CanRead && prop.CanWrite
let column = Attribute.GetCustomAttribute(prop,
typeof(ColumnAttribute))
as ColumnAttribute
where column == null || !column.IsPrimaryKey
select (MemberBinding)Expression.Bind(prop,
Expression.Property(param, prop));

cloner = Expression.Lambda<Func<T,T>>(
Expression.MemberInit(
Expression.New(typeof(T)), bindings), param).Compile();
}
public static T Clone(T obj)
{
return cloner(obj);
}

}
}
Jun 27 '08 #2
Marc,

I'm really late with my reply, but I very much appreciate your help. Your
code was very helpful.

Thanks,
Nick

"Marc Gravell" wrote:
I don't think there is anything built in; however, you can probably
automate much of the work - the following makes a fairly crude (but very
quick) shallow-copy using the default .ctor() [making a point of
ignoring the primary key field(s) - although it isn't really
LINQ-specific] - with the advantage that since this is an extension
method, you could replace on an individual basis by adding a specific
Clone() method to the partial class, and it will be
chosen by the compiler.

using System;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Linq.Expressions;
static class Program
{
static void Main()
{
Foo foo = new Foo { Id = 16, Name = "Fred", DoB = DateTime.Today };
Foo bar = foo.Clone();
}
}
class Foo
{
[Column(IsPrimaryKey = true)] // PK
public int Id { get; set; }
[Column] // test with non-PK ColumnAttribute
public string Name { get; set; }
// test w/o ColumnAttribute
public DateTime DoB { get; set; }
}
public static class ObjectExt
{
public static T Clone<T>(this T obj) where T : new()
{
return ObjectExtCache<T>.Clone(obj);
}
static class ObjectExtCache<Twhere T : new()
{
private static readonly Func<T, Tcloner;
static ObjectExtCache()
{
ParameterExpression param = Expression.Parameter(typeof(T),
"in");

var bindings = from prop in typeof(T).GetProperties()
where prop.CanRead && prop.CanWrite
let column = Attribute.GetCustomAttribute(prop,
typeof(ColumnAttribute))
as ColumnAttribute
where column == null || !column.IsPrimaryKey
select (MemberBinding)Expression.Bind(prop,
Expression.Property(param, prop));

cloner = Expression.Lambda<Func<T,T>>(
Expression.MemberInit(
Expression.New(typeof(T)), bindings), param).Compile();
}
public static T Clone(T obj)
{
return cloner(obj);
}

}
}
Jul 10 '08 #3
No problem,

Marc
Jul 11 '08 #4
Please go to http://www.a2zdotnet.com/Default.aspx
for step by step tutorials for beginers in LINQ sections.

--
regards,
Pankaj
http://www.A2ZDotNet.com
"Nick" wrote:
Hello,

I'm using LINQ to access a SQL Server database. The user needs to be able to
duplicate a record. Is there an easy way to do this? I rather not have to set
each property from one to the other. I need to copy all the relationships
too.

Thanks for any help, I really appreciate it.

Thanks,
Nick
Aug 11 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bob Johnson | last post by:
Just wondering if LINQ might be useful and appropriate in the following scenario: I'm writing a Windows Forms app that enables the user to search for a "client account". The client account is...
22
by: paululvinius | last post by:
Hi! Testing som Linq-expressions and tried to measure performance and compare it to pre-Linq programming. The folloing two methods are functional equal but the non-Linq one is twice as fast....
8
by: Alcides | last post by:
Hello all, I learn about LINQ here in this forum. I been a VB.NET programmer for quite a while and we are using an internal solution for SQL access. I have some experience with C# and I started...
5
by: Andy B | last post by:
I was just wondering, when you create dataContext methods, should you put business logic there to try and minimize pushing data through 2-3 layers of code? or should the business logic still go in...
7
by: Andy B | last post by:
Just wondering why linq is more useful than datasets? The stuff I do doesn't seem to be too complicated to use linq with it. If I did use linq with it now, I would be doing almost the exact same...
9
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Hi all: after reading different places/sites about linq... I ran into these questions: 1. What framework do we need to run linq ? (does it depend on what version of visual studio we have?) how...
5
by: ck1 | last post by:
Hi at all - I want to know if there are diffirence with Liqn to SQL and Linq To entity performance. I have read many article on the web that say that Linq To Entity is more fast than Linq To...
9
by: Cirene | last post by:
I'm about to begin a brand new, big, ASP.NET project (using 3.5 .net fw), VS 2008. I'm using MySQL as the backend (customer request.) I have absolutely no experience with LINQ and/or the Entity...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.