C# 如何使用linq获得多个结果集
我有一个存储过程,它有大约14个不同的结果集。我如何检索它们,因为现在我只得到第一个结果集C# 如何使用linq获得多个结果集,c#,linq,asp.net-web-api,C#,Linq,Asp.net Web Api,我有一个存储过程,它有大约14个不同的结果集。我如何检索它们,因为现在我只得到第一个结果集 [HttpGet] [Route("tire-tabel")] public List<DeviationCalculation_Result> TireTabel(decimal presentWidth, decimal presentAspectRatio, string presentRimSize, int maxDeviation) { using (var context
[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;
}
}
[HttpGet]
[路线(“轮胎标签”)]
公共列表TireTabel(十进制presentWidth、十进制presentspectratio、字符串presentRimSize、int max偏差)
{
使用(var context=new ominitreenties())
{
var result=context.Database.SqlQuery(
“exec[Tabel]。[DeviationCalculation]@PresentWidth='”+PresentWidth+“,”+
“@PresentAspectRatio=”+PresentAspectRatio+“,”+
“@presentiches=”+presentRimSize+“,”+
“@MaxDeviation=”“+MaxDeviation+””).ToList();
返回结果;
}
}
示例代码:
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();
}
}
使用(var db=new BloggingContext())
{
//如果首先使用代码,我们需要确保在打开连接之前构建了模型
//对于使用EF设计器创建的模型,这不是必需的
初始化(强制:false);
//创建SQL命令以执行存储过程
var cmd=db.Database.Connection.CreateCommand();
cmd.CommandText=“[dbo].[GetAllBlogsAndPosts]”;
尝试
{
db.Database.Connection.Open();
//运行存储过程
var reader=cmd.ExecuteReader();
//从第一个结果集中阅读博客
var blogs=((IObjectContextAdapter)db)
.ObjectContext
.Translate(阅读器,“博客”,MergeOption.AppendOnly);
foreach(博客中的var项)
{
Console.WriteLine(项目名称);
}
//移动到第二个结果集并阅读文章
reader.NextResult();
var posts=((IObjectContextAdapter)db)
.ObjectContext
.Translate(阅读器,“Posts”,MergeOption.AppendOnly);
foreach(POST中的var项目)
{
控制台写入线(项目名称);
}
}
最后
{
db.Database.Connection.Close();
}
}
Translate方法接受执行过程时收到的读取器、EntitySet名称和MergeOption。EntitySet名称将与派生上下文上的DbSet属性相同。MergeOption枚举控制在内存中已存在相同实体时如何处理结果
参考:
我还建议使用参数,而不是执行问题中提到的查询,因为它可以导致SQL注入,非常简单:
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,
//...
};
}
}
公共偏差计算结果获取(十进制presentWidth、十进制presentspectratio、字符串presentRimSize、int-max偏差)
{
使用(var context=new ominitreenties())
{
var reader=context.Database.Connection.QueryMultiple(“[Tabel].[DeviationCalculation]”,
新的
{
PresentWidth=PresentWidth,
PresentAspectatio=PresentAspectatio,
presentiches=presentRimSize,
最大偏差=最大偏差
},commandType:commandType.StoredProcess);
var first=reader.Read().ToList().first();
var second=reader.Read().ToList().First();
var third=reader.Read().ToList().First();
//……等等。。。
返回新的偏差计算结果
{
第一个=第一个,
秒=秒,
第三=第三,
//...
};
}
}
什么是[Tabel].[DeviationCalculation]
内的代码?您正在传递一些参数值,我认为这些参数值会进入where
条件并返回过滤结果?不完全是,它会进入数学函数以计算周长,然后返回15个预定义周长的偏差