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

时间:2017-09-04 12:03:45

标签: 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 = 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;
    }
}

2 个答案:

答案 0 :(得分: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();
    }
}

Translate方法接受我们在执行过程时收到的读者,EntitySet名称和MergeOption。 EntitySet名称将与派生上下文中的DbSet属性相同。如果内存中已存在相同的实体,则MergeOption枚举控制如何处理结果。

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

我还建议使用参数而不是执行问题中提到的查询,因为可以导致SQL注入

答案 1 :(得分: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,
            //...
        };
    }
}