locked
Get child of child entities RRS feed

  • Question

  • hi,

    in a typical :

    Customer, order and order_details relationship ,

    how can i get all the order_details for a given customer in linq ?

    thanks and good day
    Tuesday, October 21, 2014 7:02 AM

Answers

  • Hello issam1975,

    >>Customer, order and order_details relationship ,

    Are the relationships of three tables: Customer and order are one to many, order and order_details are one to many, too?

    If it is, please try LINQ query below:

    using (db_testEntities db = new db_testEntities())
    
                {
    
                    db.Database.Log = Console.Write;
    
    
                    //this is a given customer
    
                    Customer givenCustomer = db.Customers.FirstOrDefault();
    
    
                    //get all the order_details for a given customer in linq
    
                    var result = (from orderdetail in db.OrderDetails
    
                                  join order in db.Orders on orderdetail.OrderID equals order.OrderID
    
                                  where order.CustomerID == givenCustomer.CustomerID
    
                                  select orderdetail).ToList();
    
                }
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by issam1975 Tuesday, October 21, 2014 8:36 AM
    Tuesday, October 21, 2014 8:03 AM

All replies

  • Hello issam1975,

    >>Customer, order and order_details relationship ,

    Are the relationships of three tables: Customer and order are one to many, order and order_details are one to many, too?

    If it is, please try LINQ query below:

    using (db_testEntities db = new db_testEntities())
    
                {
    
                    db.Database.Log = Console.Write;
    
    
                    //this is a given customer
    
                    Customer givenCustomer = db.Customers.FirstOrDefault();
    
    
                    //get all the order_details for a given customer in linq
    
                    var result = (from orderdetail in db.OrderDetails
    
                                  join order in db.Orders on orderdetail.OrderID equals order.OrderID
    
                                  where order.CustomerID == givenCustomer.CustomerID
    
                                  select orderdetail).ToList();
    
                }
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by issam1975 Tuesday, October 21, 2014 8:36 AM
    Tuesday, October 21, 2014 8:03 AM
  • thanks Fred, it worked like expected .

    now how to use the result in a datagrid for TwoWays Binding .

    if i understand correctly the result of the above query is a copy of the actual data from the database .

    so any change to it don't affect the actual datacontext .


    • Edited by issam1975 Tuesday, October 21, 2014 8:41 AM
    Tuesday, October 21, 2014 8:40 AM
  • if i understand correctly the result of the above query is a copy of the actual data from the database .

    so any change to it don't affect the actual datacontext .

    They are just objects in memory in Object Oriented Programming. The objects are not persisted back to the underlying data stor, until you initiate programming logic that is going to persist an object to the database.

    Tuesday, October 21, 2014 2:18 PM