Guilherme Oderdenge
Guilherme Oderdenge

Reputation: 5001

Count the results from Stored Procedure

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.

Upvotes: 2

Views: 1438

Answers (2)

Flavio Canedo
Flavio Canedo

Reputation: 11

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

@ROWCOUNT INT OUTPUT

SELECT
FROM
WHERE

SET @ROWCOUNT = @@ROWCOUNT

Upvotes: 0

Eoin Campbell
Eoin Campbell

Reputation: 44308

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

Upvotes: 3

Related Questions