Tools/LinqToSqlExtensions.cs
/// <summary>
/// Returns a list of changed items inside the Context before being applied to the data store.
/// </summary>
/// <param name="context">The DataContext to interogate for pending changes.</param>
/// <returns>Returns a list of changed items inside the Context before being applied to the data store.</returns>
/// <remarks>Based on Ryan Haney's code at http://dotnetslackers.com/articles/csharp/GettingChangedEntitiesFromLINQToSQLDataContext.aspx.  Note that this code relies on reflection of private fields and members.</remarks>
Returns a list of changed items inside the Context before being applied to the data store.
Returns a list of changed items inside the Context before being applied to the data store .
public static List<ChangedItems<TEntity>> GetChangedItems<TEntity>( this DataContext context )
		{
			List<ChangedItems<TEntity>> changedItems = new List<ChangedItems<TEntity>>();
			PropertyInfo providerProperty = context.GetType().GetProperty( "Provider", BindingFlags.Instance | BindingFlags.NonPublic );
			var provider = providerProperty.GetValue( context, null );
			Type providerType = provider.GetType();
			object services = providerType.GetField( "services",
			  BindingFlags.NonPublic |
			  BindingFlags.Instance |
			  BindingFlags.GetField ).GetValue( provider );
			object tracker = services.GetType().GetField( "tracker",
			  BindingFlags.NonPublic |
			  BindingFlags.Instance |
			  BindingFlags.GetField ).GetValue( services );
			System.Collections.IDictionary trackerItems =
			  (System.Collections.IDictionary)tracker.GetType().GetField( "items",
			  BindingFlags.NonPublic |
			  BindingFlags.Instance |
			  BindingFlags.GetField ).GetValue( tracker );
			foreach ( System.Collections.DictionaryEntry entry in trackerItems )
			{
				object original = entry.Value.GetType().GetField( "original",
								  BindingFlags.NonPublic |
								  BindingFlags.Instance |
								  BindingFlags.GetField ).GetValue( entry.Value );
				if ( entry.Key is TEntity && original is TEntity )
				{
					changedItems.Add(
					  new ChangedItems<TEntity>( (TEntity)entry.Key, (TEntity)original )
					);
				}
			}
			return changedItems;
        }
public static List < ChangedItems < TEntity > > GetChangedItems < TEntity > ( this DataContext context ) { List < ChangedItems < TEntity > > changedItems = new List < ChangedItems < TEntity > > ( ) ; PropertyInfo providerProperty = context . GetType ( ) . GetProperty ( " " , BindingFlags . Instance | BindingFlags . NonPublic ) ; var provider = providerProperty . GetValue ( context , null ) ; Type providerType = provider . GetType ( ) ; object services = providerType . GetField ( " " , BindingFlags . NonPublic | BindingFlags . Instance | BindingFlags . GetField ) . GetValue ( provider ) ; object tracker = services . GetType ( ) . GetField ( " " , BindingFlags . NonPublic | BindingFlags . Instance | BindingFlags . GetField ) . GetValue ( services ) ; System . Collections . IDictionary trackerItems = ( System . Collections . IDictionary ) tracker . GetType ( ) . GetField ( " " , BindingFlags . NonPublic | BindingFlags . Instance | BindingFlags . GetField ) . GetValue ( tracker ) ; foreach ( System . Collections . DictionaryEntry entry in trackerItems ) { object original = entry . Value . GetType ( ) . GetField ( " " , BindingFlags . NonPublic | BindingFlags . Instance | BindingFlags . GetField ) . GetValue ( entry . Value ) ; if ( entry . Key is TEntity && original is TEntity ) { changedItems . Add ( new ChangedItems < TEntity > ( ( TEntity ) entry . Key , ( TEntity ) original ) ) ; } } return changedItems ; }
Returns a list of changed items inside the Context before being applied to the data store.
Returns a list of changed items inside the Context before being applied to the data store .
// create a dictionary of type TItem for return to caller // use reflection to get changed items from data context // iterate through each item in context, adding // only those that are of type TItem to the changedItems dictionary
Returns a list of changed items inside the Context before being applied to the data store.
Returns a list of changed items inside the Context before being applied to the data store .
DataContext
this
context
The DataContext to interogate for pending changes.
The DataContext to interogate for pending changes .
remarks
None