By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,179 Members | 1,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,179 IT Pros & Developers. It's quick & easy.

Copy Linq objects

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
No problem,

Marc
Jul 11 '08 #4

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.