OK, I have a bunch of complex logic I need to iterate through. I am calling a web service and getting back results. I take the results and loop through them and make different database calls based on various rules.
The initial code I used was the following:
foreach(var obj in MyObjects)
{
using(var connection = new LinqToSqlDBContext())
{
connection.StoredProc1(obj.Name);
connection.StoredProc2(obj.Name);
connection.SubmitChanges();
}
}
I figure this would be more efficient:
using(var connection = new LinqToSqlDBContext())
{
foreach(var obj in MyObjects)
{
connection.StoredProc1(obj.Name);
connection.StoredProc2(obj.Name);
}
connection.SubmitChanges();
}
Is there a better way I can do this to increase performance? I am using Linq to SQL classes and C# 4.0.
Thanks!
Since you are invoking stored-procs, but not performing object changes, the SubmitChanges()
is redundant. In normal usage, I would expect this SubmitChanges
to be a significant factor, so balancing the number of calls vs the size of individual transactions is important. However, that doesn't apply here, so I would just use:
using(var connection = new LinqToSqlDBContext())
{
foreach(var obj in MyObjects)
{
connection.StoredProc1(obj.Name);
connection.StoredProc2(obj.Name);
}
}
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