locked
How to get a whole new object from actual data but cache memory by LinQ RRS feed

  • Question

  • As the title says, when I use like DataContext.Table.Where(item => item.ID == ObjectID) or others data requirements, the same DataContext will return the Object already in the cache if exist.

    I don't wanna structure a new DataContext or XElement object sometimes, what should I do then?


    • Edited by Indream Luo Friday, October 14, 2011 8:25 AM
    • Moved by Paul Zhou Monday, October 17, 2011 8:03 AM move for better support (From:.NET Base Class Library)
    Friday, October 14, 2011 8:23 AM

Answers

  • You can use the Refresh method on the DataContext to refresh an object.
    Click the 'Vote as Helpful' arrow if this post was helpful.
    • Marked as answer by Indream Luo Tuesday, October 18, 2011 9:41 AM
    Friday, October 14, 2011 10:10 AM
  • Hi Lui;

    From the sounds of it you are using Linq to SQL and want to make a copy of a returned object that the copy is NOT pointing to the object in the DataContext, in other words a completely new object. If that is the case you can do the following. Let assume that you have a table in the database called Employee and you want to make a copy of an Employee as stated above you will need to extend the Employee class that is in the DataContext by creating a new partial class in a new file in the same namespace as the Employee in the DataContext. It should look like the following code snippet.

    namespace SameAsOtherEmployeeClass
    {
        public partial class Employee
        {
            public Employee ShallowCopy( )
            {
                return ( Employee ) this.MemberwiseClone( );
            }
        }
    }
    

    Now in code you can do the following to get a copy of the Employee object.

    DataClasses1DataContext ctx = new DataClasses1DataContext();
    
    // Query the Employee table
    var query = ctx.Employees..Where(item => item.ID == ObjectID).ToList( );
    // Make a copy of the first object in the query list.
    Employee employeeCopy = query[ 0 ].ShallowCopy( );
    


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Indream Luo Tuesday, October 18, 2011 9:41 AM
    Monday, October 17, 2011 2:31 PM

All replies

  • You can use the Refresh method on the DataContext to refresh an object.
    Click the 'Vote as Helpful' arrow if this post was helpful.
    • Marked as answer by Indream Luo Tuesday, October 18, 2011 9:41 AM
    Friday, October 14, 2011 10:10 AM
  • Will which make other items point to the same object be changed?
    Monday, October 17, 2011 6:02 AM
  • I'm moving this thread to LinQ forum to get better support.

    Have a nice day.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 17, 2011 8:03 AM
  • Thank u, wl this be top there after moving?
    Monday, October 17, 2011 9:08 AM
  • Hi Lui;

    From the sounds of it you are using Linq to SQL and want to make a copy of a returned object that the copy is NOT pointing to the object in the DataContext, in other words a completely new object. If that is the case you can do the following. Let assume that you have a table in the database called Employee and you want to make a copy of an Employee as stated above you will need to extend the Employee class that is in the DataContext by creating a new partial class in a new file in the same namespace as the Employee in the DataContext. It should look like the following code snippet.

    namespace SameAsOtherEmployeeClass
    {
        public partial class Employee
        {
            public Employee ShallowCopy( )
            {
                return ( Employee ) this.MemberwiseClone( );
            }
        }
    }
    

    Now in code you can do the following to get a copy of the Employee object.

    DataClasses1DataContext ctx = new DataClasses1DataContext();
    
    // Query the Employee table
    var query = ctx.Employees..Where(item => item.ID == ObjectID).ToList( );
    // Make a copy of the first object in the query list.
    Employee employeeCopy = query[ 0 ].ShallowCopy( );
    


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Indream Luo Tuesday, October 18, 2011 9:41 AM
    Monday, October 17, 2011 2:31 PM
  • Thank u Mr.Soto & Sorahi,

    Ways you provided are all useful in some area.

    After testing, I though I have a more complex problem, Maybe it's more funny.

    Tuesday, October 18, 2011 9:44 AM