How to get output result from stored procedure use Entity Framework in C#?

前端 未结 2 1277
太阳男子
太阳男子 2021-01-18 21:25

I\'m working on an ASP.NET MVC project; my goal is to prepare a report from a table so, the first time I\'ve wrote Linq code but it was too slow.

And after that I\'

2条回答
  •  一个人的身影
    2021-01-18 22:19

    Declare your store procedure using this syntax:

        USE yourDataBaseNAme
        GO
    
        CREATE PROCEDURE [dbo].yourStoreProcedureName
        @startDate nvarchar(30), 
        @endDate   nvarchar(30)
        AS 
           SELECT Cast(date_rec_slash as datetime) AS 'date_rec_slash', count(code_marz) as total,
                  CASE
                      WHEN code_marz = 1 THEN 'a'
                      WHEN code_marz = 2 THEN 'b'
                      WHEN code_marz = 3 THEN 'c'
                      WHEN code_marz = 4 THEN 'd'
                      WHEN code_marz = 5 THEN 'e'
                  END AS 'code_marz'
          FROM dbo.tbl_bar 
         WHERE Cast(date_rec_slash as datetime) between @startDate 
                                                    AND @endDate
         GROUP BY Cast(date_rec_slash as datetime), code_marz
         ORDER BY Cast(date_rec_slash as datetime) ASC;
        GO
    

    Call this store procedure in EF:

    db.Database.SqlQuery("yourStoreProcedureName");
    

    Call store procedure with parameter in EF:

    SqlParameter startDate= new SqlParameter("@startDate", "Value");
    SqlParameter endDate= new SqlParameter("@endDate", "Value");
    db.Database.SqlQuery("exec yourStoreProcedureName @startDate, @endDate", startDate, endDate).ToList();
    

    your Object to Cast:

    public class yourObjectNameToCAST
    {
         public datetime date_rec_slash { get; set; }
         public int total { get; set; }
         public string code_marz { get; set; }
    }
    

提交回复
热议问题