locked
How to fetch master detail data using EF and LINQ RRS feed

  • Question

  • User1183902823 posted

    suppose i have a order and order detail entity. i want to fetch specific order data and its related order detail by EF.

    can i use this LINQ query

    IEnumerable<Order> order= _ctx.order
                .Include(x => x.orderdetails)
                .Where(x => x.OrderID== _OrderID).ToList();

    does the above code works fine? also tell me how to achieve the same with join also?

    also tell me if i need to show specific customer data and customer order and order details then how i need to compose my EF query? help me with code. thanks

    Wednesday, January 3, 2018 11:37 AM

Answers

  • User-707554951 posted

    Hi tridip1974

      protected void Page_Load(object sender, EventArgs e)
            {
                using (Database3Entities _ctx = new Database3Entities())
                {
                    //does the above code works fine?          
                   Order orders = (_ctx.Orders.Include("Order_Details").Where(x => x.OrderID == 10250)).SingleOrDefault();
                    IEnumerable<Order_Detail> details = orders.Order_Details.ToList();
                    GridView1.DataSource = details;
                    GridView1.DataBind();
                    //also tell me how to achieve the same with join also?
                    var joinResult = (from ep in _ctx.Orders
                                      join d in _ctx.Order_Details on ep.OrderID equals d.OrderID
                                      where ep.OrderID == 10250
                                      select new
                                      {
                                          id = ep.OrderID,
                                          CustomerId = ep.CustomerID,
                                          UnitPrice = d.UnitPrice,
                                          Quantity = d.Quantity
                                      }).ToList();
                    GridView2.DataSource = joinResult;
                    GridView2.DataBind();
                    // if i need to show specific customer data and customer order and order details then how i need to compose my EF query? help me with code
                    var entryPoint = (from ep in _ctx.Customers
                                      join d in _ctx.Orders on ep.CustomerID equals d.CustomerID
                                      join t in _ctx.Order_Details on d.OrderID equals t.OrderID
                                      where ep.CustomerID == "ANATR"
                                      select new
                                      {
                                          CustomerID = ep.CustomerID,
                                          OrderDate = d.OrderDate,
                                          Quantity = t.Quantity
                                      }).ToList();
                    GridView3.DataSource = entryPoint;
                    GridView3.DataBind();
    
                }
            }

    Output:

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 17, 2018 10:41 AM