linq和存储过程

时间:2011-02-25 09:19:28

标签: linq stored-procedures

我正在尝试在LINQ中执行存储过程。在SQL Server中运行时,它返回多个字段,主要是整数和浮点字段。但是当我在LINQ中调用存储过程时,它返回的全部是0.以下是我调用存储过程的方式:

var meterReadings = _database.MetricDataReport("20101127");

这是存储过程:

ALTER PROCEDURE [dbo].[MetricDataReport]
@FormDate VARCHAR(8)
AS
SET NOCOUNT ON  



/*Table To Hold Finished RecordSet*/
CREATE TABLE #OutputTable
(
    PkMetricMeterID INT,
    FkSiteID        INT,
    FourWeekReading FLOAT,
    LastWeekReading FLOAT,
    ThisWeekReading FLOAT,
    pkMeterID       INT,
    DecimalPlaces   INT

)

CREATE TABLE #WorkingTable
(
    PkMetricMeterID INT,
    FkSiteID        INT,
    FormDate        DATETIME,
    Reading         FLOAT,
    pkMeterID       INT,
    DecimalPlaces   INT
)   

-----------------------------------
---   Process Physical Meters   ---
-----------------------------------
    INSERT INTO #WorkingTable (PkMetricMeterID,FkSiteID,FormDate,Reading,pkMeterID,DecimalPlaces)
    SELECT
        luMetricMeters.PkMetricMeterID,
        LuMetricMeterList.FkSiteID,
        GetReadingsAndPriceLite.FormDate,
        GetReadingsAndPriceLite.CalculatedConsumption * GetReadingsAndPriceLite.ConversionFactor,
        luMetricMeterList.pkMeterID,            
        Meters.DecimalPlaces    
    FROM
        luMetricMeters 
    INNER JOIN
        luMetricMeterList ON luMetricMeterList.PkMetricMeterID = luMetricMeters.PkMetricMeterID
    INNER JOIN  
        GetReadingsAndPriceLite ON GetReadingsAndPriceLite.pkMeterID = luMetricMeterList.pkMeterID
    INNER JOIN
        Meters ON Meters.PkMeterID = luMetricMeterList.pkMeterID
    WHERE
        GetReadingsAndPriceLite.FormDate <= @FormDate AND GetReadingsAndPriceLite.FormDate >= DATEADD(wk,-4,@FormDate)
--------------------------------------------
---   Process Virtual Meters (NON KPI)   ---
--------------------------------------------

---Check To See If Meter Needs Updating
DECLARE @CurrentMeterID INT
DECLARE MeterCursor Cursor FAST_FORWARD FOR
    SELECT 
        luMetricMeterList.pkMeterID
    FROM
        LuMetricMeters 
    INNER JOIN
        luMetricMeterList ON luMetricMeterList.PkMetricMeterID = luMetricMeters.PkMetricMeterID
    INNER JOIN
        VirtualMeterReadings ON VirtualMeterReadings.MeterID = luMetricMeterList.pkMeterID
    WHERE
        VirtualMeterReadings.UpdateFlag = 1
OPEN MeterCursor
FETCH NEXT FROM MeterCursor INTO @CurrentMeterID
WHILE @@Fetch_Status=0
BEGIN
    EXEC UpdateVirtualReadings @CurrentMeterID
    FETCH NEXT FROM MeterCursor INTO @CurrentMeterID
END
CLOSE MeterCursor
DEALLOCATE MeterCursor


INSERT INTO #WorkingTable (PkMetricMeterID,FkSiteID,FormDate,Reading,pkMeterID,DecimalPlaces)
    SELECT
        luMetricMeters.PkMetricMeterID,
        LuMetricMeterList.FkSiteID,
        VirtualMeterReadings.FormDate,
        VirtualMeterReadings.Reading,
        LuMetricMeterList.pkMeterID,
        Meters.DecimalPlaces            
    FROM
        luMetricMeters 
    INNER JOIN
        luMetricMeterList ON luMetricMeterList.PkMetricMeterID = luMetricMeters.PkMetricMeterID
    INNER JOIN  
        VirtualMeterReadings ON VirtualMeterReadings.MeterID = luMetricMeterList.pkMeterID
    INNER JOIN
        Meters ON Meters.PkMeterID = luMetricMeterList.pkMeterID
    WHERE
        VirtualMeterReadings.FormDate <= @FormDate AND VirtualMeterReadings.FormDate >= DATEADD(wk,-4,@FormDate)

SET NOCOUNT OFF

SELECT DISTINCT
    [#WorkingTable].PkMetricMeterID,
    #WorkingTable.FkSiteID,
    ThisWeekReading = (SELECT TOP 1 Reading FROM #WorkingTable WorkingTable1 WHERE WorkingTable1.PkMetricMeterID = #WorkingTable.PkMetricMeterID AND [#WorkingTable].FkSiteID = [WorkingTable1].FkSiteID ANd workingTable1.FORMDATE = @FormDate),
    LastWeekReading = (SELECT TOP 1 Reading FROM #WorkingTable WorkingTable1 WHERE WorkingTable1.PkMetricMeterID = #WorkingTable.PkMetricMeterID AND [#WorkingTable].FkSiteID = [WorkingTable1].FkSiteID ANd workingTable1.FORMDATE = DATEADD(d,-7,@FormDate)),
    FourWeekReading = (SELECT AVG(Reading) FROM #WorkingTable WorkingTable1 WHERE WorkingTable1.PkMetricMeterID = #WorkingTable.PkMetricMeterID AND [#WorkingTable].FkSiteID = [WorkingTable1].FkSiteID),
    #WorkingTable.pkMeterID,
    #WorkingTable.DecimalPlaces
FROM
    [#WorkingTable]
ORDER BY    
    [#WorkingTable].PkMetricMeterID,
    [#WorkingTable].FkSiteID

1 个答案:

答案 0 :(得分:0)

我对此知之甚少,看看问题是什么,但您可能希望首先使用更简单的存储过程来实现此功能。例如,只返回简单列表的东西。

另外,请查看使用Linq to Sql的存储过程的ScottGu's blog post