2015-10-20 59 views
2

我試圖將選擇查詢轉換爲Linq查詢,非常感謝任何幫助。將選擇語句轉換爲Linq查詢

SQL查詢

SELECT Orders.CustomerID, Orders.OrderID, Link_OrderProduct.LinkID AS  Link_OrderProduct, Garages.GarageID, Products.ProductID, Garages.GarageName,VehicleDetails.Registration, Link_OrderProduct.FittingDate, Products.Brand, Products.TyreModel, Link_OrderProduct.ProductQuantity, 
         Products.ProductUnitSalePrice, Link_OrderProduct.TotalProductSaleCost, Link_OrderProduct.ReferralFee 

FROM   Link_OrderProduct INNER JOIN 
         Orders ON Link_OrderProduct.OrderID = Orders.OrderID INNER JOIN 
         Products ON Link_OrderProduct.ProductID = Products.ProductID INNER JOIN 
         Customers ON Orders.CustomerID = Customers.CustomerID INNER JOIN 
         VehicleDetails ON Customers.CustomerID = VehicleDetails.CustomerID INNER JOIN 
         Suppliers ON Products.SupplierID = Suppliers.SupplierID INNER JOIN 
         Link_GarageSupplier INNER JOIN 
         Garages ON Link_GarageSupplier.GarageID = Garages.GarageID ON Suppliers.SupplierID = Link_GarageSupplier.SupplierID 
WHERE  (Orders.Paid IS NULL) AND (Link_OrderProduct.Cancelled IS NULL) 

這是我使用LINQ

var query = from Link_OrderProduct in dbcontext.Link_OrderProduct 

    join Order in dbcontext.Orders on Link_OrderProduct.LinkID equals Order.OrderID 
    join Products in dbcontext.Products on Link_OrderProduct.LinkID equals Products.ProductID 
    join Customers in dbcontext.Orders on Order.CustomerID equals Customers.CustomerID 
    join VehicleDetails in dbcontext.Customers on Customers.CustomerID equals VehicleDetails.CustomerID 
    join Suppliers in dbcontext.Products on Products.SupplierID equals Suppliers.SupplierID 

        //where d.UserID == userID 
        select Order; 
+0

在dbcontext.Link_GarageSupplier加入LGS上lgs.SupplierId等於Suppliers.SupplierID參加研討會在Garages.GarageID dbcontext.Garages等於lgs.GarageID –

+0

也可以使用一些工具(如[linqpad](http://www.linqpad.net/))將您的sql代碼轉換爲linq代碼 –

回答

0

到目前爲止管理當且僅當你有外鍵正確設置你能做到這樣,而不是使用SQL像語法:

dbContext.Order 
     .Where(z=> z.Paid == null 
        && z.Link_OrderProduct.Cancelled == null) 
     .Select(z=> new() { 
           CustomerID = z.CustomerID, 
           OrderID = z.OrderID, 
           Link_OrderProduct = z.Link_OrderProduct.LinkID, 
           GarageID = z.Link_OrderProduct.Product.Supplier.Link_GarageSupplier.GarageID, 
           ProductID = GarageID = z.Link_OrderProduct.ProductID, 
           GarageName = z.Link_OrderProduct.Product.Supplier.Link_GarageSupplier.Garage.GarageName 
           //here you go for other select stuff 
          } 
       ); 

如果你有大部分的外鍵,但缺少一個或兩個,你可以還做這樣的事情(例如假設,我們缺少Link_OrderProduct和產品之間的外鍵):

dbContext.Order 
      .Join(
         Product, 
         order => order.Link_OrderProduct.ProductID, 
         product => product.ProductID, 
         (order, product) => new() {Order = order, Product = product} 
        ) 
      .Where(z=> z.Paid == null 
         && z.Link_OrderProduct.Cancelled == null) 
      .Select(z=> new() { 
             CustomerID = z.Order.CustomerID, 
             OrderID = z.Order.OrderID, 
             Link_OrderProduct = zOrder..Link_OrderProduct.LinkID, 
             GarageID = z.Product.Supplier.Link_GarageSupplier.GarageID, 
             ProductID = z.Product.ProductID , 
             GarageName = z.Product.Supplier.Link_GarageSupplier.Garage.GarageName 
             //here you go for other select stuff           } 
        );