You could use the dataContext.GetChangeSet()
to track all the changed entities and filter the specific T
entities. Please see if this is what you want:
public static void ShowModifiedEntitiesInfo<T>(DataContext context) where T : class
{
foreach (var entity in context.GetChangeSet().Updates.Where(del => del is T).Cast<T>())
{
ModifiedMemberInfo[] modifiedMembers = context.GetTable<T>().GetModifiedMembers(entity);
Console.WriteLine("Updated Entity: " + entity.ToString());
Console.WriteLine(" (Members Changed)");
foreach (var member in modifiedMembers)
{
Console.WriteLine(" - Member Name: " + member.Member.Name);
Console.WriteLine(" - Original Value: " + member.OriginalValue.ToString());
Console.WriteLine(" - Current Value: " + member.CurrentValue.ToString());
}
}
foreach (var entity in context.GetChangeSet().Inserts.Where(del => del is T).Cast<T>())
{
Console.WriteLine("Inserted Entity: " + entity.ToString());
}
foreach (var entity in context.GetChangeSet().Deletes.Where(del => del is T).Cast<T>())
{
Console.WriteLine("Deleted Entity: " + entity.ToString());
}
}
EDIT:
Is what you need something like this?
public static void ShowModifiedCustomerInfo(MyDataContext context, Customer customer)
{
ModifiedMemberInfo[] modifiedMembers = context.Customers.GetModifiedMembers(customer);
List<Order> updatedOrders = context.GetChangeSet().Updates.Where(e => e is Order).Cast<Order>().Intersect<Order>(customer.Orders).ToList<Order>();
List<Order> insertedOrders = context.GetChangeSet().Inserts.Where(e => e is Order).Cast<Order>().Intersect<Order>(customer.Orders).ToList<Order>();
List<Order> deletedOrders = context.GetChangeSet().Deletes.Where(e => e is Order).Cast<Order>().Intersect<Order>(customer.Orders).ToList<Order>();
if (modifiedMembers.Length > 0 || updatedOrders.Count > 0 || insertedOrders.Count > 0 || deletedOrders.Count > 0)
{
Console.WriteLine("Updated Customer: " + customer.ToString());
foreach (var member in modifiedMembers)
{
Console.WriteLine(" - Member Name: " + member.Member.Name);
Console.WriteLine(" - Original Value: " + member.OriginalValue.ToString());
Console.WriteLine(" - Current Value: " + member.CurrentValue.ToString());
}
foreach (var entity in updatedOrders)
{
Console.WriteLine(" Updated Order: " + entity.ToString());
}
foreach (var entity in insertedOrders)
{
Console.WriteLine(" Inserted Order: " + entity.ToString());
}
foreach (var entity in deletedOrders)
{
Console.WriteLine(" Deleted Order: " + entity.ToString());
}
}
}
The Customer
is the entity and has an EntitySet<Order>
. For what I understand, you what to know if the customer itself has changed and if there was any order from this customer that also changed.