Grupos de Google ya no admite publicaciones ni suscripciones nuevas de Usenet. El contenido anterior sigue visible.

Copy Linq objects

7,039 vistas
Ir al primer mensaje no leído

Nick

no leída,
5 may 2008, 2:27:00 p.m.5/5/2008
para
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

Marc Gravell

no leída,
6 may 2008, 3:20:15 a.m.6/5/2008
para
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<T> where T : new()
{
private static readonly Func<T, T> cloner;
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);
}

}
}

Nick

no leída,
10 jul 2008, 4:57:01 p.m.10/7/2008
para
Marc,

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

Thanks,
Nick

Marc Gravell

no leída,
11 jul 2008, 2:39:23 a.m.11/7/2008
para
No problem,

Marc


Se borró el mensaje
0 mensajes nuevos