1
我最近开发了一个使用Linq的C#应用程序。 我从外部数据库获取我需要处理的配置文件列表,其中一些是新的,一些已经在数据库中,需要更新。 我今天做的是通过配置文件列表并检查每个配置文件,如果存在我更新,否则我插入 - 此解决方案工作正常。Visual Studio C#Linq批量插入/更新
我确定有一种方法可以像使用UPDATE ON DUPLICATE一样使用批量插入/更新,这样我可以节省时间,因为我得到的文件非常庞大,并且已知批量插入/更新具有更好的性能。我想避免我现在使用的迭代。
insertall不与已经存储行工作,我需要两个更新的组合,并插入
这里是我的代码,你的帮助是高度赞赏。
foreach (Profile tmpProfile in profiles)
{
try
{
var matchedProfile = (from c in db.ProfileEntities
where c.ProfileId == tmpProfile.Id
select c).SingleOrDefault();
if (matchedProfile == null)
{
//Insert
db.ProfileEntities.InsertOnSubmit(EntityMapper.ToEntity(tmpProfile));
}
else
{
//Update
EntityMapper.ToEntity(ref matchedProfile, tmpProfile);
}
}
catch (System.Data.SqlServerCe.SqlCeException sqlExec)
{
}
catch (Exception e)
{
}
}
db.SubmitChanges();
这意味着我将不得不遍历的项目清单,以确定哪些位于数据库中,然后通过一个更新一个,你认为这样更好吗? – user149318 2009-08-03 06:15:39