如何处理实体框架中存储过程的动态结果?

时间:2019-04-01 10:54:22

标签: c# stored-procedures entity-framework-6

我有一个存储过程,该过程根据提供的输入参数提供不同的结果。我需要执行该过程并将结果转换为数据表以显示在网格中。我怎样才能做到这一点?我需要为所有结果创建一个模型还是有通用的方法?

--There are lots of report type. Just listed 2 of them for now

CREATE PROCEDURE [dbo].[uspGetAdminReportData]
@reportType VARCHAR(50)
AS 
BEGIN
    IF(@reportType = 'TOTAL_BY_DATE')
    BEGIN
        SELECT CONVERT(VARCHAR, DATE_SENT, 111) AS Date_Sent, FORMAT(DATE_SENT, 'dddd') AS Day_Of_Week, COUNT(*) AS Email_Count FROM TBLDATA1 WITH(NOLOCK) GROUP BY DATE_SENT
    END
    ELSE IF (@reportType = 'USER_LOGIN_LOGOUT_REPORT')
    BEGIN
        SELECT UserName, CONVERT(VARCHAR(10), [LOGIN], 111) AS LOGIN_DATE, CONVERT(VARCHAR(3),(SUM(DATEDIFF(MINUTE,[LOGIN],[LOGOUT]))/60)) + ':' + RIGHT('00' + CONVERT(VARCHAR(3),(SUM(DATEDIFF(MINUTE,[LOGIN],[LOGOUT])) - (SUM(DATEDIFF(MINUTE,[LOGIN],[LOGOUT]))/60) * 60)),2) AS TOTAL_TIME FROM USERAUDIT WITH(NOLOCK)
        INNER JOIN 
        UserRecords WITH(NOLOCK) ON UserAudit.UserId = UserRecords.UserId
        Where DateDiff(Minute, [Login], [LOGOUT]) > 0 
        GROUP BY UserRecords.UserName, CONVERT(VARCHAR(10), [LOGIN], 111)
        ORDER BY UserRecords.UserName, CONVERT(VARCHAR(10), [LOGIN], 111)
    END
END

C# code
 public virtual DataTable GetAdminReportData(ReportType reportType)
 {
    //not sure how to get the result executing the procedure for various report types. Need to execute the procedure, and cast the result as a data table and return to load table to the grid using entity framework

    SqlParameter ReportType = new SqlParameter("reportType", reportType.ToString());
    var result = this.Database.SqlQuery<object>("uspGetAdminReportData @reportType", ReportType);

 }

0 个答案:

没有答案