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