日期:2014-05-20 浏览次数:21073 次
int r3加入量 = 0, r4更新量 = 0;
foreach (var item in Students)
{
Student r2= dc.Student.SingleOrDefault(o => o.bbid == item.bbid);
if (r2!=null)
{
r2 = item;
r4更新量++;
}
else
{
r2 = item;
dc.Student.InsertOnSubmit(r2);
r3加入量++;
}
}
dc.SubmitChanges();
p1加入量 = r3加入量;
p2更新量 = r4更新量;
private static void AddRange<TEntity>(ObjectContext context,
IEnumerable<TEntity> entities,
out int adding, out int updating)
where TEntity : class
{
var table = context.CreateObjectSet<TEntity>();
adding = 0;
updating = 0;
foreach (var entity in entities)
{
var key = context.CreateEntityKey(table.Name, entity);
var original = context.GetObjectByKey(key) as TEntity;
if (original == null)
{
table.AddObject(entity);
adding++;
}
else
{
context.ObjectStateManager.GetObjectStateEntry(key).CurrentValues.SetValues(entity);
updating++;
}
}
}