简体   繁体   中英

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 ?

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.

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);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM