2014-09-30 94 views
0

我对ASP.NET/MVC/LINQ很新。我在我的数据库三个表,并在我的解决方案及其相应的模型:Linq MVC查看模型

salesperson 
    id 
, name 
, address 
, dob 

sales 
    id 
, shopid 
, salespersonid 
, productid 
, AmountSale 
, saleDate 

Shop 
    id 
, shopName 
, Location 

我也有一个存储过程将返回下面的所有销售人员的数据:

SalesPersonid 
, TotalAmount_Sale 
, Most_Recent_ShopName 
, Count_Of_sales_Lifetime 
, count_of_sales_ThisMonth 

如何调用存储过程使用LINQ并在前端显示数据?到目前为止,我所见过的所有样本都会返回一个已经存在的模型。我很困惑,请帮忙。

+0

您在使用任何类型的ORM的? (实体框架,NHibernate等) – 2014-09-30 20:23:57

+0

你需要使用存储过程吗? – SethMW 2014-09-30 20:24:24

+1

通常你不会使用存储过程,你通常会使用实体框架ORM。 [使用实体框架调用存储过程](http://visualstudiomagazine.com/articles/2014/04/01/calling-stored-procedures-from-entity-framework.aspx)是绝对有可能的,但它通常是你做的事情很少,只是为了优化特定情况。 – 2014-09-30 20:28:16

回答

2

假设你不介意添加库到组合:

首先,进入安装Dapper dot Net。然后,建立一个模型来存储您的结果:

class MySprocResult 
{ 
    public int SalesPersonid { get; set; } 
    public decimal TotalAmount_Sale { get; set; } 
    public string Most_Recent_ShopName { get; set; } 
    public int Count_Of_sales_Lifetime { get; set; } 
    public int count_of_sales_ThisMonth { get; set; } 
} 

接下来,建立一个数据库连接,并使用小巧玲珑点网返回结果:

String connectionString = "connectionString"; 
using (IDbConnection db = new System.Data.SqlClient.SqlConnection(connectionString)) 
{ 
    // Open SQL connection 
    db.Open(); 

    // Fetch results from stored procedure 
    IEnumerable<MySprocResult> results = db.Query<MySprocResult>(
     "mySprocName",       // stored procedure name 

     //--- OPTIONAL 
     //param: new { id = 123 },    // pass parameters if necessary 
     //--- /OPIONAL 

     commandType: CommandType.StoredProcedure // tell dapper it's a SPROC 
    ); 

    // Close SQL connection 
    db.Close(); 

    /* work with "results" here */ 
} 
+0

+1,我喜欢Dapper,它非常快速且易于使用 – 2014-09-30 20:37:49