简体   繁体   English

如何使用linq获取多个结果集

[英]how to get multiple result sets using linq

I have a stored procedure with around 14 different result sets. 我有一个带有约14个不同结果集的存储过程。 How do I retrieve them all as by now I only get the first result set. 我如何检索它们,因为到现在为止我只得到第一个结果集。

[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;
    }
}

Sample Code: 样例代码:

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();
    }
}

The Translate method accepts the reader that we received when we executed the procedure, an EntitySet name, and a MergeOption. Translate方法接受执行过程时收到的阅读器,EntitySet名称和MergeOption。 The EntitySet name will be the same as the DbSet property on your derived context. EntitySet名称将与派生上下文上的DbSet属性相同。 The MergeOption enum controls how results are handled if the same entity already exists in memory. 如果内存中已经存在相同的实体,则MergeOption枚举控制如何处理结果。

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

I also recommend to use Parameters instead of executing the queries as mentioned in the question as it can result in SQL injection 我还建议使用“参数”而不是执行问题中提到的查询,因为它可能导致SQL注入

With Dapper it is super simple: 使用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,
            //...
        };
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM