Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 26, 2012 07:50 AM by patuary
Member
118 Points
428 Posts
Apr 24, 2012 05:44 PM|LINK
Hi,
I want copy one table content to another table with account table accountid.
My account table has 300 record, i want copy vendorproduct table records to product table 300 times with each account
Here is my code, But this code is very slow, How can i speed up this process?
public ActionResult AddBulkProduct() { var accountList = db.Accounts.ToList(); foreach(var accoun in accountList ) { var vProducts = db.VendorProducts.ToList(); foreach (var Vproduct in vProducts) { Product product = new Product(); product.Id = Guid.NewGuid(); product.ProductName = Vproduct.ProductName; product.ItemNumber = Vproduct.ItemNumber; product.VendorId = Vproduct.VendorId; product.Barcode = Vproduct.BarCode; product.AccountId = accoun.Id ; product.CurrentPrice = Vproduct.CurrentPrice; db.Products.Add(product); } } db.SaveChanges(); return Content("Suceesfull"); }
Contributor
4394 Points
1102 Posts
Apr 24, 2012 05:46 PM|LINK
Do it in SQL Server.
3970 Points
1096 Posts
Apr 24, 2012 06:01 PM|LINK
gslakmal I want copy one table content to another table with account table accountid. My account table has 300 record, i want copy vendorproduct table records to product table 300 times with each account
Take a day off and then look at your database design. It might have room for improvement.
All-Star
118619 Points
18779 Posts
Apr 26, 2012 01:46 AM|LINK
gslakmal foreach (var Vproduct in vProducts) { Product product = new Product(); product.Id = Guid.NewGuid(); product.ProductName = Vproduct.ProductName; product.ItemNumber = Vproduct.ItemNumber; product.VendorId = Vproduct.VendorId; product.Barcode = Vproduct.BarCode; product.AccountId = accoun.Id ; product.CurrentPrice = Vproduct.CurrentPrice; db.Products.Add(product); }
Hi:)
I wonder why you are using looping?It seems that you are adding duplicated records into the same table by assigning existing things' values for a new one and add it into the same table ……Why?Would you plz tell us what you are thinking of?
Reguards!
Apr 26, 2012 03:01 AM|LINK
I want dublicate all product record for each account
Apr 26, 2012 05:41 AM|LINK
gslakmal I want dublicate all product record for each account
It seems that you have too many records……Maybe you have to use SqlCommand instead of LINQ to speed up……
425 Points
143 Posts
Apr 26, 2012 07:50 AM|LINK
if u are using framework 4.0 then try inner loop for parallelism.
like this
foreach(var accoun in accountList )
{
var vProducts = db.VendorProducts.ToList();
Parallel.ForEach(vProducts ,Vproduct =>{
Product product = new Product();
product.Id = Guid.NewGuid();
product.ProductName = Vproduct.ProductName;
product.ItemNumber = Vproduct.ItemNumber;
product.VendorId = Vproduct.VendorId;
product.Barcode = Vproduct.BarCode;
product.AccountId = accoun.Id ;
product.CurrentPrice = Vproduct.CurrentPrice;
db.Products.Add(product);
});
}
db.SaveChanges();
return Content("Suceesfull");
gslakmal
Member
118 Points
428 Posts
bulk copy
Apr 24, 2012 05:44 PM|LINK
Hi,
I want copy one table content to another table with account table accountid.
My account table has 300 record, i want copy vendorproduct table records to product table 300 times with each account
Here is my code, But this code is very slow, How can i speed up this process?
public ActionResult AddBulkProduct() { var accountList = db.Accounts.ToList(); foreach(var accoun in accountList ) { var vProducts = db.VendorProducts.ToList(); foreach (var Vproduct in vProducts) { Product product = new Product(); product.Id = Guid.NewGuid(); product.ProductName = Vproduct.ProductName; product.ItemNumber = Vproduct.ItemNumber; product.VendorId = Vproduct.VendorId; product.Barcode = Vproduct.BarCode; product.AccountId = accoun.Id ; product.CurrentPrice = Vproduct.CurrentPrice; db.Products.Add(product); } } db.SaveChanges(); return Content("Suceesfull"); }adamturner34
Contributor
4394 Points
1102 Posts
Re: bulk copy
Apr 24, 2012 05:46 PM|LINK
Do it in SQL Server.
Dan Bracuk
Contributor
3970 Points
1096 Posts
Re: bulk copy
Apr 24, 2012 06:01 PM|LINK
Take a day off and then look at your database design. It might have room for improvement.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: bulk copy
Apr 26, 2012 01:46 AM|LINK
Hi:)
I wonder why you are using looping?It seems that you are adding duplicated records into the same table by assigning existing things' values for a new one and add it into the same table ……Why?Would you plz tell us what you are thinking of?
Reguards!
gslakmal
Member
118 Points
428 Posts
Re: bulk copy
Apr 26, 2012 03:01 AM|LINK
I want dublicate all product record for each account
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: bulk copy
Apr 26, 2012 05:41 AM|LINK
It seems that you have too many records……Maybe you have to use SqlCommand instead of LINQ to speed up……
patuary
Member
425 Points
143 Posts
Re: bulk copy
Apr 26, 2012 07:50 AM|LINK
if u are using framework 4.0 then try inner loop for parallelism.
like this
foreach(var accoun in accountList )
{
var vProducts = db.VendorProducts.ToList();
Parallel.ForEach(vProducts ,Vproduct =>{