Linq,内部Join,Group By,Order By的实体框架

时间:2016-11-28 16:21:15

标签: sql entity-framework linq

SQL代码:

SELECT   
            ProductsExportID, Fname, Lname, InsuranceName,
            Orders.ProductID, ProductName, Price,
            SUM(PrTotalExport) AS PRCuont,
            SUM(Price) AS PRPrice,
            SUM(Price * PrTotalExport) AS TotalPrice
        FROM 
            ProductsExport
        INNER JOIN 
            Orders ON Orders.OrdersID = ProductsExport.OrdersID
        INNER JOIN 
            Products ON Products.ProductID = ProductsExport.ProductID
        INNER JOIN 
            Customer ON Customer.CustomerID = Orders.CustomerID
        INNER JOIN 
            Insurance ON Insurance.InsuranceID = Customer.InsuranceID
        GROUP BY 
            ProductsExportID, Fname, Lname, InsuranceName,
            Orders.ProductID, ProductName, Price, PrTotalExport, 
            ProductsExport.OrdersID
        ORDER BY 
            ProductID

我尝试过以下LINQ代码:

var res = from e in db.ProductsExports
                      join o in db.Orders on e.OrderID equals o.OrderID
                      join c in db.Customers on o.CustomerID equals c.CustomerID
                      join p in db.Products on o.ProductID equals p.ProductID
                      join i in db.Insurances on c.InsuranceID equals i.InsuranceID

                      select new { e.ProductsExportID, c.Fname, c.Lname, p.ProductID, p.Price, e.PrTotalExport } into x
                      group x by new { x.ProductsExportID, x.Fname, x.Lname, x.ProductID, x.Price, x.PrTotalExport }into g

                      select new
                      {
                          gProductsExportID = g.ProductsExportID,
                          gFname = g.Key.Fname,
                          gLname=g.Key.Lname,
                          gprid=g.Key.ProductID,
                          gprice=g.Key.Price,
                          gTotalpr=g.Key.PrTotalExport,
                            gTotal=g.Sum(p=>p.Price)

                      };

我不知道是谁用连接和小组来解决这个问题因为我一直在做一个小组而我无法访问另一个     客户和产品。

0 个答案:

没有答案