2014-09-30 93 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