将select语句转换为Linq查询

时间:2015-10-20 20:55:34

标签: sql 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;

1 个答案:

答案 0 :(得分: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和Product之间缺少外键):

    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                                            }
                    );