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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With