关闭

Linq 中的IMultipleResult的用于调用存储过程获取多个结果集(表)

1658人阅读 评论(0) 收藏 举报

 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  
END  
 

 

  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存储过程大概就是这么几步.也不是太困难!

 

* 当把存储过程作为数据库对象添加到.dbml文件中式,为该存储过程映射到DataContext的子类中函数的时候,默认的是返回ISingleResult类型,而且会生成一个相应的实体类,这个实体类用于设置ISingleResult类型中元素的类型:private ISingleResult<实体类型> Pr_GetUsers(){ }, ISingleResult<实体类型> result = dbContext.Pr_GetUsers();;当存储过程返回的是多个结果集时(返回单个结果集时也可以设置),同时也需要多个结果集时,就应该手动更改存储过程的映射函数的返回类型为IMultileResults,而且需要通过ResultType Attribute,设置返回若干结果集中每个结果集的类型。IMultipleResult result = dbContext.Pr_GetUsersAndRoles(); IEnumerable users = result.GetResult<User>(); IEnumerable roles = result.GetResult<Role>();

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:56755次
    • 积分:521
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:6篇
    • 译文:0篇
    • 评论:19条
    文章分类