Copy Linq objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Tmljaw==?=

    #1

    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
  • Marc Gravell

    #2
    Re: Copy Linq objects

    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.Lin q.Mapping;
    using System.Linq;
    using System.Linq.Exp ressions;
    static class Program
    {
    static void Main()
    {
    Foo foo = new Foo { Id = 16, Name = "Fred", DoB = DateTime.Today };
    Foo bar = foo.Clone();
    }
    }
    class Foo
    {
    [Column(IsPrimar yKey = 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( )
    {
    ParameterExpres sion param = Expression.Para meter(typeof(T) ,
    "in");

    var bindings = from prop in typeof(T).GetPr operties()
    where prop.CanRead && prop.CanWrite
    let column = Attribute.GetCu stomAttribute(p rop,
    typeof(ColumnAt tribute))
    as ColumnAttribute
    where column == null || !column.IsPrima ryKey
    select (MemberBinding) Expression.Bind (prop,
    Expression.Prop erty(param, prop));

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

    }
    }

    Comment

    • =?Utf-8?B?Tmljaw==?=

      #3
      Re: Copy Linq objects

      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.Lin q.Mapping;
      using System.Linq;
      using System.Linq.Exp ressions;
      static class Program
      {
      static void Main()
      {
      Foo foo = new Foo { Id = 16, Name = "Fred", DoB = DateTime.Today };
      Foo bar = foo.Clone();
      }
      }
      class Foo
      {
      [Column(IsPrimar yKey = 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( )
      {
      ParameterExpres sion param = Expression.Para meter(typeof(T) ,
      "in");
      >
      var bindings = from prop in typeof(T).GetPr operties()
      where prop.CanRead && prop.CanWrite
      let column = Attribute.GetCu stomAttribute(p rop,
      typeof(ColumnAt tribute))
      as ColumnAttribute
      where column == null || !column.IsPrima ryKey
      select (MemberBinding) Expression.Bind (prop,
      Expression.Prop erty(param, prop));
      >
      cloner = Expression.Lamb da<Func<T,T>>(
      Expression.Memb erInit(
      Expression.New( typeof(T)), bindings), param).Compile( );
      }
      public static T Clone(T obj)
      {
      return cloner(obj);
      }
      >
      }
      }
      >

      Comment

      • Marc Gravell

        #4
        Re: Copy Linq objects

        No problem,

        Marc


        Comment

        • =?Utf-8?B?UGFua2Fq?=

          #5
          RE: Copy Linq objects

          Please go to http://www.a2zdotnet.com/Default.aspx
          for step by step tutorials for beginers in LINQ sections.

          --
          regards,
          Pankaj



          "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

          Comment

          Working...