曾經被linq存儲過程返回多個結果所困擾,今晚加班,問題解決,分享一下思路:
linq存儲過程默認生成的代碼是ISingleResult的,也就是只能返回一條結果集,我們先動手腳,將其改 成IMultipleResults 的.實體類根據不同情況更改.
linq存儲過程更改前:
[Function(Name="dbo.MeterTaskStat")]
public ISingleResult<XXXX> MeterTaskStat
([Parameter(Name="MeterTaskType", DbType="Int")]
System.Nullable<int> meterTaskType,
[Parameter(Name="StartDate", DbType="DateTime")]
System.Nullable<System.DateTime> startDate,
[Parameter(Name="EndDate", DbType="DateTime")]
System.Nullable<System.DateTime> endDate)
{
IExecuteResult result =
this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())),
meterTaskType, startDate, endDate);
return ((ISingleResult<XXXXXX>)(result.ReturnValue));
}
linq存儲過程更改後:
[Function(Name="dbo.MeterTaskStat")]
[ResultType(typeof(TaskStatData))]
public IMultipleResults MeterTaskStat
([Parameter(Name = "MeterTaskType", DbType = "Int")]
System.Nullable<int> meterTaskType,
[Parameter(Name = "StartDate", DbType = "DateTime")]
System.Nullable startDate,
[Parameter(Name = "EndDate", DbType = "DateTime")]
System.Nullable endDate)
{
IExecuteResult result =
this.ExecuteMethodCall
(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())),
meterTaskType, startDate, endDate);
return ((IMultipleResults)(result.ReturnValue));
}
注意到 多一條: [ResultType(typeof(TaskStatData))] 的記錄吧,簡單介紹一下,必須得為linq存儲 過程的結果返回一個實體類型,而TaskStatData就是自己定義的類, [ResultType(typeof (TaskStatData))]必須加上,加linq存儲過程回值.
linq存儲過程:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- ===================================================
-- Author:MaHong
-- Create date: 2008-09-11
-- Description: 根據口徑統計某段時間內水表復裝任務信息
-- ===================================================
ALTER PROCEDURE [dbo].[MeterTaskStat]
@MeterTaskType INT,
@StartDate DateTime,
@EndDate DateTime
AS
BEGIN
SET NOCOUNT ON;
SELECT MeterCaliberName,SUM(Requisition) AS
RequisitionCount,SUM(Approve) AS ApproveCount,
SUM(Disapprove) AS DisapproveCount,SUM(WaitWork) AS WaitWorkCount,
SUM(CompleteY) AS CompleteYCount,SUM(CompleteN) AS CompleteNCount,
SUM(Requisition+Approve+Disapprove+WaitWork+CompleteY+CompleteN) AS
Subtotal
FROM (SELECT MeterCaliberName
,CASE WHEN MeterTaskStatus=0 THEN 1 ELSE 0 END Requisition
,CASE WHEN MeterTaskStatus=1 THEN 1 ELSE 0 END Approve
,CASE WHEN MeterTaskStatus=11 THEN 1 ELSE 0 END Disapprove
,CASE WHEN MeterTaskStatus=2 THEN 1 ELSE 0 END WaitWork
,CASE WHEN MeterTaskStatus=4 THEN 1 ELSE 0 END CompleteY
,CASE WHEN MeterTaskStatus=5 THEN 1 ELSE 0 END CompleteN
FROM View_MeterTaskMaintain WHERE [MeterTaskType] =
@MeterTaskType AND StartDate BETWEEN @StartDate AND @EndDate) tempTable
GROUP BY MeterCaliberName
linq存儲過程之在business中間層直接調用:
public class StatTaskControl : ControlBase
{
public IEnumerable GetStatInfo
(TaskType type, DateTime startDate, DateTime endDate)
{
IMultipleResults info =
Context.MeterTaskStat((int)type, startDate, endDate);
IEnumerable data = info.GetResult();
return data;
}
}
linq存儲過程之ui層獲取:
protected void StatButton_Click(object sender, EventArgs e)
{
DateTime startDate = DateTime.Parse(StartDate.Text);
DateTime endDate = DateTime.Parse(EndDate.Text);
TaskType type = TaskType.Remove;
IEnumerable info =
_control.GetStatInfo(type, startDate, endDate);
List data = info.ToList();
RemoveGridView.DataSource = data;
RemoveGridView.DataBind();
}
整個linq存儲過程大概就是這麼幾步.也不是太困難!