2

The goal

Count the results returned from a stored procedure.

The problem

I have the following code on my ProductsController:

[HttpGet]
public ActionResult DailyOffers()
{
    var productsList = Products.BuildOffersList();
    ViewBag.Title = String.Format("Ofertas de hoje ({0:dd/MM/yyyy})", 
                                                   DateTime.Now);
    ViewBag.CategoryProductsQuantity = ??;
    ViewBag.CurrentCategory = "Daily-Offers";
    return View(productsList);
}

As you can see, there is a builder on this method. This builder returns the Stored Procedure result. And I want to count the number of results that this procedure returns.

What I'm thinking about

Maybe this?:

ViewBag.CategoryProductsQuantity = productsList.Count;

Technical details

I'm using C#.NET + MySql + Entity Framework 5 + Razor Engine.

4

2 回答 2

3

Assuming that Products.BuildOffersList(); does in fact return a List (and not an IEnumerable/IQueryable) then what you've suggested should be fine and won't result in multiple enumerations.

 ViewBag.CategoryProductsQuantity = productsList.Count();
于 2013-06-17T12:59:56.033 回答
0

Use parameter "output" on Stored Procedure and create parameter "out" on your method.

@ROWCOUNT INT OUTPUT

SELECT
FROM
WHERE

SET @ROWCOUNT = @@ROWCOUNT
于 2013-06-17T13:00:40.517 回答