Join two tables using LINQ Query and order based two parameters

前端 未结 3 928
深忆病人
深忆病人 2021-01-18 04:15

I have two tables Customers and Orders. I want a LINQ query to fetch list of all the orders placed by all the customers organized first by month and then by year. If there i

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-18 04:58

    This is what you can do:

    var res = from cust in db.Customers 
              join ord in db.Orders 
                   on cust.customer_id equals ord.customer_id into g 
              from d in g.DefaultIfEmpty()
              orderby d.OrderDate.Year, d.OrderDate.Month
              select new { 
                   name=cust.name, 
                   oId = d.order_id.ToString() ?? "No Orders" 
              };
    

提交回复
热议问题