locked
Update Master/Detail tables! RRS feed

  • Question

  •  

    Hello,every one,i have 2 questions:

    1.  A "Product" table, and "PurchaseOrderDetail" table,"PurchaseOrderDetail" table's fields:

    purchaseorderdetailid,purchaseorderid,productid,price,quantity,totalamount.

    when i show the "PurchaseOrderDetail" table,i wanna show "PruductName".so How to write the Lamda?

    My self's solution that create a view,then load to the "viewPurchaseOrderDetail",but "PurchaseOrder" and "viewPurchaseOrderDetail" has no relations!so i think it is not a good solution.

     

     

     

    2. How to update "PurchaseOrder" and "PurchaseOrderDetail" tables?becuase at question1, for show "ProductName",that i use view,so how to update it?

     

    Thanks!

    Thursday, September 25, 2008 8:14 AM

Answers

  • Hi

    first of all make a partial class for PurchaseOrderDetail, as you know there is a partial class for each entity in datacontext.designer.cs. after that make propety for returning product name.

    Be consider that your entities( product & PurchaseOrderDetail have an association via productid). 

    public Partial Class PurchaseOrderDetail
    {
    public string ProductName
    {
    get
    {
    return this.Product.Name
    }
    }
    }

    you dont need to use lambada but if you want to use it:
    for example :

    product p = db.Products.Single(c => c.ID == 1);

    so you dont need to use view, so you can update your PurchaseOrder & PurchaseOrderDetail easily.







    Thursday, September 25, 2008 9:14 AM
  • Hi CSQ333,


    For question 1, query data from two tables (“Product” and “PurchaseOrderDetail”) on the “ProductID” field, perhaps you can take a look at the following code snippet.


      MyDataContext db = new MyDataContext();

                     var queries = from a in db.Products

                                   from b in db.PurchaseOrderDetails

                                   where a.ProductID == b.ProductID

                                   select new { PID = a.ProductID, PName=a.ProductName, SomeOtherField=b.SomeOtherField};


    To update a record in a table, first of all, query the record first, then modify the query result, at the end, submit the changes, please refer to the following sample.


      MyDataContext db = new MyDataContext();

                     var query = db.Products.First(e => e.ProductID == 1);

              a.ProductName = “Hello world”;                   

    db.SubmitChanges(); //no submit, the changes will never write back to the database


    In addition to this, please refer to the following blogs.

    LINQ to SQL : Update data through Object Model

    LINQ to SQL Tips 7


    Regards,

    Xun

    Monday, September 29, 2008 5:41 AM

All replies

  • Hi

    first of all make a partial class for PurchaseOrderDetail, as you know there is a partial class for each entity in datacontext.designer.cs. after that make propety for returning product name.

    Be consider that your entities( product & PurchaseOrderDetail have an association via productid). 

    public Partial Class PurchaseOrderDetail
    {
    public string ProductName
    {
    get
    {
    return this.Product.Name
    }
    }
    }

    you dont need to use lambada but if you want to use it:
    for example :

    product p = db.Products.Single(c => c.ID == 1);

    so you dont need to use view, so you can update your PurchaseOrder & PurchaseOrderDetail easily.







    Thursday, September 25, 2008 9:14 AM
  • hi Ali Asghar Alimardani , thanks for your reply.

    But how do i know which detail records were deleted?and update?and insert?

     

    thanks.

    Friday, September 26, 2008 12:41 AM
  • Hi CSQ333,


    For question 1, query data from two tables (“Product” and “PurchaseOrderDetail”) on the “ProductID” field, perhaps you can take a look at the following code snippet.


      MyDataContext db = new MyDataContext();

                     var queries = from a in db.Products

                                   from b in db.PurchaseOrderDetails

                                   where a.ProductID == b.ProductID

                                   select new { PID = a.ProductID, PName=a.ProductName, SomeOtherField=b.SomeOtherField};


    To update a record in a table, first of all, query the record first, then modify the query result, at the end, submit the changes, please refer to the following sample.


      MyDataContext db = new MyDataContext();

                     var query = db.Products.First(e => e.ProductID == 1);

              a.ProductName = “Hello world”;                   

    db.SubmitChanges(); //no submit, the changes will never write back to the database


    In addition to this, please refer to the following blogs.

    LINQ to SQL : Update data through Object Model

    LINQ to SQL Tips 7


    Regards,

    Xun

    Monday, September 29, 2008 5:41 AM