开发者

How to get the affected rows number of Linq To Sql?

How to get the affected rows number of Linq To Sql?

I use Linq To Sql to delete a batch of records in the sql server 2005.

How can I get the affected rows number?

And How can I know how many records have been deleted if that is something wro开发者_开发技巧ng happened in the batch delete?


using (DataContext db = new DataContext())
{
    db.Stuff.InsertOnSubmit(stuff);
    int rowsInserted = db.GetChangeSet().Inserts.Count;
    db.SubmitChanges();
}

The GetChangeSet() method also contains updates & deletes, if that's what you need.


Below is an example of a function that would get the number of rows affected.

public int updateDB(int ID){
        List<Person> people = db.Person.Where(i => i.ID > ID).ToList();

        foreach(Person p in people)
            p.Name = "John";

        return db.SaveChanges(); //return the number of rows affected
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜