none
Update All Fields RRS feed

  • Question

  • Hi NG,

     

    I'm using LINQ to SQL with following code:

    Code Snippet

    SampleDbDataContext db = new SampleDbDataContext();

    db.Log = Console.Out;

    Person oldPerson = db.Persons.SingleOrDefault(p => p.Name == "Aytekin");

    Person newPerson = db.Persons.SingleOrDefault(p => p.Name == "Tekeli");

     

    oldPerson.Name = newPerson.Name;

    oldPerson.Age = newPerson.Age;

    db.SubmitChanges();

     

     

    With this code I can transfer newPersons data to oldPerson and so I can update my old person record in my database.

     

    In some cases, my object (database table) have more than 30 fields.

     

    Is there any ways (without defining each fields which I want to update) to update my record in my database like following code?

    Code Snippet

    SampleDbDataContext db = new SampleDbDataContext();

    db.Log = Console.Out;

    Person oldPerson = db.Persons.SingleOrDefault(p => p.Name == "Aytekin");

    Person newPerson = db.Persons.SingleOrDefault(p => p.Name == "Tekeli");

    oldPerson = newPerson;

    db.SubmitChanges();

     

     

    Best,

    Özgür Aytekin

     

     

     

    Thursday, July 24, 2008 6:34 PM

Answers

  • There is no direct support in LINQ to SQL for copying fields/properties over from one object to another.

     

    However, you can use the classes in System.Reflection to write a general-purpose method that will do so. (Note that you probably want to skip the key properties.)

     

    Thanks,

     

    --Samir

     

    Thursday, July 24, 2008 10:41 PM

All replies

  • There is no direct support in LINQ to SQL for copying fields/properties over from one object to another.

     

    However, you can use the classes in System.Reflection to write a general-purpose method that will do so. (Note that you probably want to skip the key properties.)

     

    Thanks,

     

    --Samir

     

    Thursday, July 24, 2008 10:41 PM
  • Hi Samir

     

    Thank you for your answer.

     

    This can not be the solution but I'll try it using reflection.

     

    Best,

    Ozgur Aytekin

    Sunday, July 27, 2008 7:47 PM