這篇文章將為大家詳細(xì)講解有關(guān)如何實(shí)現(xiàn)linq存儲(chǔ)過(guò)程返回多條結(jié)果集,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
創(chuàng)新互聯(lián)建站專(zhuān)業(yè)為企業(yè)提供林周網(wǎng)站建設(shè)、林周做網(wǎng)站、林周網(wǎng)站設(shè)計(jì)、林周網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、林周企業(yè)網(wǎng)站模板建站服務(wù),10多年林周做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
linq存儲(chǔ)過(guò)程默認(rèn)生成的代碼是ISingleResult的,也就是只能返回一條結(jié)果集,我們先動(dòng)手腳,將其改成IMultipleResults 的.實(shí)體類(lèi)根據(jù)不同情況更改.
linq存儲(chǔ)過(guò)程更改前:
[Function(Name="dbo.MeterTaskStat")] public ISingleResultMeterTaskStat([Parameter(Name="MeterTaskType", DbType="Int")] System.Nullable 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 ((ISingleResult )(result.ReturnValue)); }
linq存儲(chǔ)過(guò)程更改后:
[Function(Name="dbo.MeterTaskStat")] [ResultType(typeof(TaskStatData))] public IMultipleResults MeterTaskStat([Parameter(Name = "MeterTaskType", DbType = "Int")] System.NullablemeterTaskType, [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))] 的記錄吧,簡(jiǎn)單介紹一下,必須得為linq存儲(chǔ)過(guò)程的結(jié)果返回一個(gè)實(shí)體類(lèi)型,而TaskStatData就是自己定義的類(lèi),[ResultType(typeof(TaskStatData))]必須加上,加linq存儲(chǔ)過(guò)程回值.
linq存儲(chǔ)過(guò)程:
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go -- =================================================== -- Author:MaHong -- Create date: 2008-09-11 -- Description: 根據(jù)口徑統(tǒng)計(jì)某段時(shí)間內(nèi)水表復(fù)裝任務(wù)信息 -- =================================================== 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 END
linq存儲(chǔ)過(guò)程之在business中間層直接調(diào)用:
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存儲(chǔ)過(guò)程之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(); }
整個(gè)linq存儲(chǔ)過(guò)程大概就是這么幾步.也不是太困難!
關(guān)于如何實(shí)現(xiàn)linq存儲(chǔ)過(guò)程返回多條結(jié)果集就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。