2017-09-04 51 views
0

我有一個包含大約14個不同結果集的存儲過程。我如何檢索它們,因爲現在我只能得到第一個結果集。如何獲得多個結果集使用linq

[HttpGet] 
[Route("tire-tabel")] 
public List<DeviationCalculation_Result> TireTabel(decimal presentWidth, decimal presentAspectRatio, string presentRimSize, int maxDeviation) 
{ 
    using (var context = new OminiTireEntities()) 
    { 
     var result = context.Database.SqlQuery<DeviationCalculation_Result>(
"exec [Tabel].[DeviationCalculation] @PresentWidth = '" + presentWidth + "', " + 
"@PresentAspectRatio= '" + presentAspectRatio + "', " + 
"@PresentInches= '" + presentRimSize + "', " + 
"@MaxDeviation= '" + maxDeviation + "'").ToList<DeviationCalculation_Result>(); 
     return result; 
    } 
} 
+0

'[Tabel]。[DeviationCalculation]'裏面的代碼是什麼?你傳遞了幾個參數值,我認爲它們會進入你的'where'條件並返回你的過濾結果? –

+0

不完全是用於計算周長的數學函數,然後返回15個預定義周長的偏差 –

回答

1

示例代碼:

using (var db = new BloggingContext()) 
{ 
    // If using Code First we need to make sure the model is built before we open the connection 
    // This isn't required for models created with the EF Designer 
    db.Database.Initialize(force: false); 

    // Create a SQL command to execute the sproc 
    var cmd = db.Database.Connection.CreateCommand(); 
    cmd.CommandText = "[dbo].[GetAllBlogsAndPosts]"; 

    try 
    { 

     db.Database.Connection.Open(); 
     // Run the sproc 
     var reader = cmd.ExecuteReader(); 

     // Read Blogs from the first result set 
     var blogs = ((IObjectContextAdapter)db) 
      .ObjectContext 
      .Translate<Blog>(reader, "Blogs", MergeOption.AppendOnly); 


     foreach (var item in blogs) 
     { 
      Console.WriteLine(item.Name); 
     }   

     // Move to second result set and read Posts 
     reader.NextResult(); 
     var posts = ((IObjectContextAdapter)db) 
      .ObjectContext 
      .Translate<Post>(reader, "Posts", MergeOption.AppendOnly); 


     foreach (var item in posts) 
     { 
      Console.WriteLine(item.Title); 
     } 
    } 
    finally 
    { 
     db.Database.Connection.Close(); 
    } 
} 

的翻譯方法接受當我們執行的過程,一個EntitySet的名稱和MergeOption,我們接收到的讀出器。 EntitySet名稱將與派生環境中的DbSet屬性相同。 MergeOption枚舉控制結果如何處理,如果相同的實體已經存在於內存中。

參考:https://msdn.microsoft.com/en-us/library/jj691402(v=vs.113).aspx

我也建議使用參數,而不是在問題中提到,因爲它可能導致SQL注入

0

隨着Dapper它是超級簡單執行查詢:

public DeviationCalculationResult Get(decimal presentWidth, decimal presentAspectRatio, string presentRimSize, int maxDeviation) 
{ 
    using (var context = new OminiTireEntities()) 
    { 
     var reader = context.Database.Connection.QueryMultiple("[Tabel].[DeviationCalculation]", 
      new 
      { 
       PresentWidth = presentWidth, 
       PresentAspectRatio = presentAspectRatio, 
       PresentInches = presentRimSize, 
       MaxDeviation = maxDeviation 
      }, commandType: CommandType.StoredProcedure); 

     var first = reader.Read<First>().ToList().First(); 
     var second = reader.Read<Second>().ToList().First(); 
     var third = reader.Read<Third>().ToList().First(); 
     //...and so on... 

     return new DeviationCalculationResult 
     { 
      First = first, 
      Second = second, 
      Third = third, 
      //... 
     }; 
    } 
}