views:

43

answers:

1

How do I update two tables at the same time using Linq-to-SQL?

var z = from a in db.Products
                join b in db.ProductSubcategories on 
                     a.ProductSubcategoryID  equals b.ProductSubcategoryID
                join d in db.ProductCategories on 
                     b.ProductCategoryID equals d.ProductCategoryID
                select new { ProductName = a.Name, ProductCategory = d.Name,     
                             ProductSubCategory = b.Name, Cost = a.StandardCost, 
                             discontinuedDate = a.DiscontinuedDate,     
                             ProductId=a.ProductID };
+1  A: 

You have to update individual records from each table and then execute db.SubmitChanges();

In your query the output is an anonymous type, not a table type connected to the db context.

If you think in terms of SQL, linq2sql works pretty much the same. You can select a record set with a join, but you cannot update directly on this. You need to break it up and modify entries directly on Products, ProductCategories and ProductSubCategories, which equals the tables in your database.

If you want to modify a Product in Products then you have to modify the properties of that type, and not the anonymous type (joined type).

Mikael Svenson