views:

136

answers:

1

Hi Experts, I have two tables in a LINQ to SQL dbml file. I am trying to get all the changes submitted to context, which I can get by using

var myOtherContext;
var changes = context.GetChangeSet();

foreach (var change in changes)
{         
    Type t = change.GetType();
    if (change is Product )
    {
        myOtherContext.GetTable<Product>().InsertOnSubmit((Product) change);
    }
    if (change is Supplier)
    {
        myOtherContext.GetTable<Supplier>().InsertOnSubmit((Supplier)change);
    }
}

Basically, I am copying all the changes that happened on one context to the other context (myOtherContext). I don't want to use an if statement to check the type of change. Is there any way I can write:

myOtherContext.GetTable<typeof(change)>().InsertOnSubmit((typeof(change) change);

Your help will be appreciated.

Regards, Parminder

+2  A: 

How about using the non-generic version:

foreach (var change in changes.Inserts) // note inserts
{         
    myOtherContext.GetTable(change.GetType()).InsertOnSubmit(change);
}
Marc Gravell
Except that wont work for derived types :)
leppie
Worked like charm. Thanks a lot.
Parminder