您的位置:首页 > 数据库

Linq to Sql 合并数据

2016-03-18 15:09 351 查看
一张表的Insert,Upadate,Delete 如何用Linq To Sql 简洁的完成

两种方法

1.使用临时表,先将数据插入到临时表中,再Merge到源表

An ORM is the wrong tool for performing batch operations, and Linq-to-SQL is no exception. In this case I think you have picked the right solution: Store all entries in a temporary table quickly, then do the UPSERT using merge.

The fastest way to store the data to the temporary table is to use SqlBulkCopy to
store all data to a table of your choice.

2.代码如下

foreach (var line in linesFromService) {
var kill = db.Kills.FirstOrDefault(t=>t.ToonId==line.ToonId && t.BossId==line.BossId);
if (kill == null) {
kill = new Kills() { ToonId = line.ToonId, BossId = line.BossId };
db.Kills.InsertOnSubmit(kill);
}
kill.LastKillTime = line.LastKillTime;
}
db.SubmitChanges();

问答链接:http://stackoverflow.com/questions/3188075/do-merge-using-linq-to-sql
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linq to sql merge