传递参数以查看Linq到SQL c#

时间:2018-01-19 07:15:47

标签: c# linq-to-sql

我已经在SQL server中的不同表中创建了一个视图。它有开始日期和结束日期作为参数。我正在使用Linq to SQL。如何将参数传递给Linq to SQL中的视图。我知道在Linq to SQL中我们可以通过在设计器上拖动它来使用与表相同的视图,但我不知道如何将参数传递给Linq to SQL中的视图。我创建的视图如下:

 SELECT        MyTable.Order_ID, MyTable.Customer_ID, MyTable.DeliveredQuantity, dbo.ReceivedAmount_ByOrder.Received_Amount, dbo.PendingAmount_ByOrder.Pending_Amount, Order_1.Total_Quantity, 
                         dbo.Customer.Name, Order_1.RatePerTon, Order_1.Discount, Order_1.Payment_Mode, Order_1.Status AS Amount_status, dbo.Material.Name AS Material_Name, dbo.Material.Type, dbo.Material.Bulk_Bags, 
                         Order_1.Tax, Order_1.Added_Value, Order_1.Final_Cost
FROM            dbo.[Order] AS Order_1 RIGHT OUTER JOIN
                             (SELECT        dbo.[Order].Order_ID, MAX(dbo.[Order].Customer_ID) AS Customer_ID, SUM(dbo.Dispatch.Full_Weight - dbo.Dispatch.Empty_Weight) AS DeliveredQuantity
                               FROM            dbo.Dispatch LEFT OUTER JOIN
                                                         dbo.Demand ON dbo.Dispatch.Demand_ID = dbo.Demand.ID LEFT OUTER JOIN
                                                         dbo.[Order] ON dbo.Demand.Order_ID = dbo.[Order].Order_ID
                               WHERE        (dbo.Dispatch.Status = N'بازرسی خروجی')
AND (dbo.Dispatch.Date>= @StartDate )  AND (dbo.Dispatch.Date<= @EndDate )
                               GROUP BY dbo.[Order].Order_ID) AS MyTable ON Order_1.Order_ID = MyTable.Order_ID LEFT OUTER JOIN
                         dbo.Customer ON Order_1.Customer_ID = dbo.Customer.ID LEFT OUTER JOIN
                         dbo.Material ON Order_1.Material_ID = dbo.Material.ID LEFT OUTER JOIN
                         dbo.ReceivedAmount_ByOrder ON MyTable.Order_ID = dbo.ReceivedAmount_ByOrder.Order_ID LEFT OUTER JOIN
                         dbo.PendingAmount_ByOrder ON MyTable.Order_ID = dbo.PendingAmount_ByOrder.Order_ID

1 个答案:

答案 0 :(得分:1)

您可以创建一个包含参数的存储过程。例如

CREATE PROCEDURE YOUR_SP_NAME
    @StartDate DATETIME,
    @EndDate DATETIME
AS
BEGIN 
    SELECT        MyTable.Order_ID, MyTable.Customer_ID, MyTable.DeliveredQuantity, dbo.ReceivedAmount_ByOrder.Received_Amount, dbo.PendingAmount_ByOrder.Pending_Amount, Order_1.Total_Quantity, 
                 dbo.Customer.Name, Order_1.RatePerTon, Order_1.Discount, Order_1.Payment_Mode, Order_1.Status AS Amount_status, dbo.Material.Name AS Material_Name, dbo.Material.Type, dbo.Material.Bulk_Bags, 
                 Order_1.Tax, Order_1.Added_Value, Order_1.Final_Cost
    FROM            dbo.[Order] AS Order_1 RIGHT OUTER JOIN
                     (SELECT        dbo.[Order].Order_ID, MAX(dbo.[Order].Customer_ID) AS Customer_ID, SUM(dbo.Dispatch.Full_Weight - dbo.Dispatch.Empty_Weight) AS DeliveredQuantity
                       FROM            dbo.Dispatch LEFT OUTER JOIN
                                                 dbo.Demand ON dbo.Dispatch.Demand_ID = dbo.Demand.ID LEFT OUTER JOIN
                                                 dbo.[Order] ON dbo.Demand.Order_ID = dbo.[Order].Order_ID
                       WHERE        (dbo.Dispatch.Status = N'بازرسی خروجی')
    AND (dbo.Dispatch.Date>= @StartDate )  AND (dbo.Dispatch.Date<= @EndDate )
                       GROUP BY dbo.[Order].Order_ID) AS MyTable ON Order_1.Order_ID = MyTable.Order_ID LEFT OUTER JOIN
                 dbo.Customer ON Order_1.Customer_ID = dbo.Customer.ID LEFT OUTER JOIN
                 dbo.Material ON Order_1.Material_ID = dbo.Material.ID LEFT OUTER JOIN
                 dbo.ReceivedAmount_ByOrder ON MyTable.Order_ID = dbo.ReceivedAmount_ByOrder.Order_ID LEFT OUTER JOIN
                 dbo.PendingAmount_ByOrder ON MyTable.Order_ID = dbo.PendingAmount_ByOrder.Order_ID
END

并将其称为:

EXEC YOUR_SP_NAME 'start_date_here', 'end_date_here'

这是从存储过程中检索值的guide