Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly InsertAllOnSubmit() and is that better than looping InsertOnSubmit()?

Say I have:

using (SomeDataContext db = new SomeDataContext())
{

     foreach(Item i in Items)
     {
          DbItem d = new DbItem;
          d.value = i.value;
          //.... etc ...

          db.InsertOnSubmit(d);
     }

     db.SubmitChanges();
}

Is it possible and/or better (worse?) to do:

using (SomeDataContext db = new SomeDataContext())
{
     IEnumerable<DbItem> dbItems = //???? possible?

     foreach(Item i in Items)
     {
          DbItem d = new DbItem;
          d.value = i.value;
          //.... etc ...

          dbItems.Add(d); // ???? again, somehow possible?
     }

     db.InsertAllOnSubmit(dbItems);
     db.SubmitChanges();
}
like image 952
MetaGuru Avatar asked Nov 22 '11 20:11

MetaGuru


1 Answers

Well you could use List<T>:

using (SomeDataContext db = new SomeDataContext())
{
     List<DbItem> dbItems = new List<DbItem>();

     foreach(Item i in Items)
     {
          DbItem d = new DbItem;
          d.value = i.value;
          //.... etc ...

          dbItems.Add(d);
     }

     db.InsertAllOnSubmit(dbItems);
     db.SubmitChanges();
}

Whether that will be any more efficient or not, I don't know.

like image 131
Jon Skeet Avatar answered Nov 11 '22 00:11

Jon Skeet