Search code examples
c#performanceentity-relationshipbltoolkit

How to update collection via BlToolkit


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?


Solution

  • 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
    }