Dimitar
Dimitar

Reputation: 484

Difference between LinqToSQL InsertOnSubmit and InsertAllOnSubmit

I have a question about LINQ to SQL.

What is faster:

 public void CreateLocationImages(IEnumerable<LocationImage> list)
    {

        _db.LocationImages.InsertAllOnSubmit(list);

        _db.SubmitChanges();

    }

or

 public void CreateLocationImages(IEnumerable<LocationImage> list)
    {   
        foreach (LocationImage item in list)
        {
            _db.LocationImages.InsertOnSubmit(item);
        }
        _db.SubmitChanges();
    }

or maybe there is no difference ?

Upvotes: 1

Views: 2453

Answers (2)

Habib
Habib

Reputation: 223312

Since in both cases you are calling SubmitChanges only once. Both of the code would result in same performance. (if there is going to be any performance different that should be negligible) If your 2nd code segment has _db.SubmitChanges(); inside the for loop then it would be a separate connection and insert statement in the db.

Upvotes: 2

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50752

it is almost the same, ILSpy is a great tool in this cases

// System.Data.Linq.Table<TEntity>
public void InsertAllOnSubmit<TSubEntity>(IEnumerable<TSubEntity> entities) where TSubEntity : TEntity
{
    if (entities == null)
    {
        throw Error.ArgumentNull("entities");
    }
    this.CheckReadOnly();
    this.context.CheckNotInSubmitChanges();
    this.context.VerifyTrackingEnabled();
    List<TSubEntity> list = entities.ToList<TSubEntity>();
    using (List<TSubEntity>.Enumerator enumerator = list.GetEnumerator())
    {
        while (enumerator.MoveNext())
        {
            TEntity entity = (TEntity)enumerator.Current;
            this.InsertOnSubmit(entity);
        }
    }
}

Upvotes: 1

Related Questions