publicclass ClientSideContext : IDisposable { publicclass StateEntries<T> { public List<T> Originals { get; set; } public List<T> Current { get; set; } } public ClientSideContext() { ctx =new DataContext("", new AttributeMappingSource()); ctx.DeferredLoadingEnabled =false; } publicvoid AttachAll<T>(IEnumerable<T> entities) where T : class { ctx.GetTable<T>().AttachAll(entities); } publicvoid Attach<T>(T t) where T : class { ctx.GetTable<T>().Attach(t); } publicvoid Remove<T>(T t) where T : class { ctx.GetTable<T>().DeleteOnSubmit(t); } publicvoid Add<T>(T t) where T : class { ctx.GetTable<T>().InsertOnSubmit(t); } public List<T> GetInserted<T>() where T : class { return (GetChangeEntries<T>(ch => ch.Inserts)); } public StateEntries<T> GetDeleted<T>() where T : class { return (GetStateEntries<T>(ch => ch.Deletes)); } private StateEntries<T> GetStateEntries<T>( Func<ChangeSet, IEnumerable<Object>> entry) where T : class { List<T> current = GetChangeEntries<T>(entry); List<T> originals = GetOriginals<T>(current); return (new StateEntries<T>() { Originals = originals, Current = current }); } public StateEntries<T> GetModified<T>() where T : class { return (GetStateEntries<T>(ch => ch.Updates)); } publicvoid Dispose() { ctx.Dispose(); } List<T> GetChangeEntries<T>( Func<ChangeSet, IEnumerable<Object>> selectMember) where T : class { var query = from o in selectMember(ctx.GetChangeSet()) where ((o as T) !=null) select (T)o; return (new List<T>(query)); } List<T> GetOriginals<T>(List<T> current) where T : class { List<T> originals =new List<T>( from c in current select ctx.GetTable<T>().GetOriginalEntityState(c)); return (originals); } private DataContext ctx; }