Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to SQL, InsertOnSubmit vs. InsertAllOnSubmit performance?

Is there a huge difference in performance between the two, for instance I have these two code snippets:

public void Insert(IEnumerable<ManageGeofenceViewModel> geofences)
{
    var insertGeofences = new List<Geofence>();

    foreach(var geofence in geofences)
    {
        Geofence insertGeofence = new Geofence
        {
            name = geofence.Name,
            radius = geofence.Meters,
            latitude = geofence.Latitude,
            longitude = geofence.Longitude,
            custom_point_type_id = geofence.CategoryID
        };

        insertGeofences.Add(insertGeofence);

    }

    this.context.Geofences.InsertAllOnSubmit(insertGeofences);
    this.context.SubmitChanges();
}

vs

public void Insert(IEnumerable<ManageGeofenceViewModel> geofences)
{
    foreach(var geofence in geofences)
    {
        Geofence insertGeofence = new Geofence
        {
            name = geofence.Name,
            radius = geofence.Meters,
            latitude = geofence.Latitude,
            longitude = geofence.Longitude,
            custom_point_type_id = geofence.CategoryID
        };
        this.context.Geofences.InsertOnSubmit(insertGeofence);
    }
    this.context.SubmitChanges();
}

Which among the two is better and does the two code snippet have the same number of trips to the database since in the 1st snippet submitchanges was called outside of the loop?

like image 435
Randel Ramirez Avatar asked Jun 20 '13 09:06

Randel Ramirez


1 Answers

There is no difference at all, InsertAllOnSubmit actually calls the InsertOnSubmit, here is the code for InsertAllSubmit:

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)((object)enumerator.Current);
            this.InsertOnSubmit(entity);
        }
    }
}

As you can see InsertAllOnSubmit is just a handy wrapper around InsertOnSubmit

like image 186
Alexandr Mihalciuc Avatar answered Oct 30 '22 02:10

Alexandr Mihalciuc