1

I have collection of users identified by property Id and for each user I want to update his username to something like username+"!".

In bltoolkit I try it like this:

using(var db = new DbManager)
{
    foreach(var user in users)
    {
        db.GetTable<User>().Where(x=>x.Id == user.Id).Set(x=>x.Username, x.Username + "!").Update();
    }        
}

I suppose that this would make n queries to the database (while n is the size of the users collection), which is what I am trying to avoid.

Is there any other (better) solution to update whole collection like this?

Xaruth
  • 4,034
  • 3
  • 19
  • 26
John Smith
  • 1,783
  • 6
  • 22
  • 36

1 Answers1

4

Add this using to your code:

using BLToolkit.Data.Linq

Now you can use Update<T> extension method in your DbManager. This method allows you to do batch update by passing collection of entities as a parameter.

In your case use it like this:

using(var db = new DbManager)
{
    foreach(var user in users)
       user.Username = user.Username + "!";  //update each user in collection first

    db.Update<User>(users);  //just one database call for batch update
}
Łukasz W.
  • 9,538
  • 5
  • 38
  • 63