用法
 public partial class Form1 : Form
    
{
        
public Form1()
        
{
            InitializeComponent();
        }

        
private ObjectSet<MachineCategory> objectSet;
        
private void button1_Click(object sender, EventArgs e)
        
{
            
using (NmisDataContext db = new NmisDataContext())
            
{
                objectSet 
= new ObjectSet<MachineCategory>(db.MachineCategories.ToList());
                bindingSource1.DataSource 
= objectSet;
                dataGridView1.DataSource 
= bindingSource1;
            }


        }


        
private void button2_Click(object sender, EventArgs e)
        
{
            MessageBox.Show(objectSet.Context.GetModified
<MachineCategory>().Current.Count.ToString());
            MessageBox.Show(objectSet.Context.GetDeleted
<MachineCategory>().Current.Count.ToString());

            MessageBox.Show(objectSet.Context.GetInserted
<MachineCategory>().Count.ToString());

        }

    }


1. 自定义的从IBindingList继承的类
public class ObjectSet<TEntity> : System.ComponentModel.BindingList<TEntity> where TEntity :class
    
{

        
private ClientSideContext context;
        
public ClientSideContext Context
        
{
            
get return context; }
            
        }

        
public ObjectSet(IList<TEntity> entities):base(entities)
        
{
            context 
= new ClientSideContext();
            context.AttachAll
<TEntity>(entities);
        
            
        }


        
protected override void InsertItem(int index, TEntity item)
        
{
            
base.InsertItem(index, item);
            Context.Add
<TEntity>(item);
        }

      
       
        
      
        
protected override void RemoveItem(int index)
        
{
           
            Context.Remove
<TEntity>(this[index]);
            
base.RemoveItem(index);
        }

      

2. 老外blog上的ClientSideDataContext
 public class ClientSideContext : IDisposable
    
{
        
public class StateEntries<T>
        
{
            
public List<T> Originals getset; }
            
public List<T> Current getset; }
        }

        
public ClientSideContext()
        
{
            ctx 
= new DataContext(""new AttributeMappingSource());
            ctx.DeferredLoadingEnabled 
= false;
        }

        
public void AttachAll<T>(IEnumerable<T> entities) where T : class
        
{
            ctx.GetTable
<T>().AttachAll(entities);
        }

        
public void Attach<T>(T t) where T : class
        
{
            ctx.GetTable
<T>().Attach(t);
        }

        
public void Remove<T>(T t) where T : class
        
{
            ctx.GetTable
<T>().DeleteOnSubmit(t);
        }

        
public void 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));
        }

        
public void 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;
    }

  


posted on 2007-12-26 20:20  jjxstudio  阅读(323)  评论(0编辑  收藏  举报