2017-09-04 49 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, 
      //... 
     }; 
    } 
}