locked
Question about item count in a result RRS feed

  • Question

  • User1670624291 posted

    The answers I found are very scattered, so I'll ask.
    Is it possible to count the number of items within the return view (result) or even in a variable?

    Thanks,

    Wednesday, February 17, 2021 4:59 PM

Answers

  • User1312693872 posted

    Hi,MiguelMi

    my purpose is for the SUM to be declared in the controller

    Could you please share the models design? To sum in controller, you can do this like this:

    SUM=db.table.select(a=>a.Values).Sum();

    Then you can pass it to view by using ViewData.

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 19, 2021 3:36 AM

All replies

  • User475983607 posted

    The answers I found are very scattered, so I'll ask.
    Is it possible to count the number of items within the return view (result) or even in a variable?

    You did not share the code that defines or populates "results".  If "results" is a collection then you can get the count of items at any time using the standard .Count() method.

    int myCount = result.Count();

    If you need the count within the View then "results" becomes the strongly typed view model.  The Razor syntax is shown below. 

    <div> My Count = @Model.Count()</div>

    Wednesday, February 17, 2021 7:30 PM
  • User1670624291 posted

    thanks, i will try to apply the example.
    my result comes from a join.

    https://forums.asp.net/t/2174364.aspx?Select+values+from+another+class

    Wednesday, February 17, 2021 9:43 PM
  • User1670624291 posted

    Yes, I managed to adapt the example to the project and it worked.

    Take the same topic to ask another question with the same theme:

    How can I count the values that are being listed in the @item.Values?

    Example:

    I am listing the values through a foreach

    Total: 412

    thanks again

    Thursday, February 18, 2021 5:26 PM
  • User475983607 posted

    Again, you have not shared any relevant code.  The example is a SUM not a COUNT which can be easily coded using LINQ if we had the actual code...

    Thursday, February 18, 2021 7:44 PM
  • User1670624291 posted

    yes, you're right i'm sorry and my example is complex

    my model from table Quantities

     public partial class Quantities
        {
            public int ID_Order { get; set; }
            public int ID_Line { get; set; }
            public Nullable<int> Value{ get; set; }
    
        }

    Controller

     public ActionResult Index()
            {
                var orders = db.Orders.ToList();
                var colors = db.Colors.ToList();
                var quantities = db.Quantities.ToList();
                //Query syntax
                var result1 = (from o in orders
                              join c in colors
                                 on o.ID_Orders equals c.ID_Orders into co
                              group new { o, co } by o.ID_Orders into g
                              from i in g
                              select new {order=i.o,colors=i.co}).ToList();
                var result2 = (from r1 in result1
                              from c in r1.colors
                               join q in quantities                              on new { orderId = c.ID_Orders, colorlineId = c.ID_Line_Color } equals new { orderId = q.ID_Orders, colorlineId = q.ID_Color_Line } into q
                              group new ColorsWithQuantitiesViewModel { coler=c, quantities=q.ToList()}  by c.ID_Orders).ToList();
                var result = (from r1 in result1
                              join r2 in result2
                                 on r1.order.ID_Orders equals r2.Key
                             select new OrderWithColorsQuantitiesViewModel
                             {
                                 order=r1.order,
                                 colers=r2.ToList()
                             }).ToList();
                return View(result);
            }

    view

    <table class="table table-borderless table-sm " id="ordertable">
        @foreach (var item in Model)
        {      
            <tr>
                <td style="width: 300px;">
                    <b>Artigo: </b>@item.order.ID_Orders
                </td>
                <td style="width: 500px;">
                    <b>Modelo: </b>@item.order.Model
                </td>
            </tr>
            <tr>
                <td colspan="8">
                    <table class="table table-bordered ordertable">
                        @foreach (var item2 in item.colers)
                        {
                            <tr>
                                @if (item2.quant.Count != 0)
                                {
                                    <td style="width: 100px;">
                                        @item2.coler.Cor
                                    </td>
                                    <td style="width: 80px;">
                                        @foreach (var item3 in item2.quant)
                                        {
                                            @item3.Values
                                        }
                                    </td>
                                }

    my values are being listed under @item3.Valuesvar sumVal= result.Sum(x => x.Value);

    my purpose is for the SUM to be declared in the controller

    Thanks

    Thursday, February 18, 2021 9:05 PM
  • User1312693872 posted

    Hi,MiguelMi

    my purpose is for the SUM to be declared in the controller

    Could you please share the models design? To sum in controller, you can do this like this:

    SUM=db.table.select(a=>a.Values).Sum();

    Then you can pass it to view by using ViewData.

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 19, 2021 3:36 AM
  • User1670624291 posted

    I have a problem with my SUM () it is only sum the first value of List to listing in a foreach
    my example:

    var totaQuantV = ResultList .Select(a => a.colers[0].Quant[0].Values).Sum();

    are there any other ways to declare a SUM?

    I tried but I have the same result;

    var totaQuantV = ResultList.Sum(a => a.colers[0].Quant[0].Values);

    thanks,

    Tuesday, February 23, 2021 7:55 PM
  • User475983607 posted

    I have a problem with my SUM () it is only sum the first value of List to listing in a foreach
    my example:

    var totaQuantV = ResultList .Select(a => a.colers[0].Quant[0].Values).Sum();

    are there any other ways to declare a SUM?

    The results are expected since you are only summing one item or one item in the nested collection.  I'm pretty sure you have design bugs related to the LINQ that generates ResultList.

    Tuesday, February 23, 2021 9:29 PM
  • User1670624291 posted

    public class OrdersColorsViewModel
        {
            public Programa order { get; set; }
            public List<ColorsAndQuantities> colors { get; set; }
            public Malha fabric { get; set; }
            public Preco_Malha price{ get; set; }
        }
    public class ColorsAndQuantities
        {
            public Programa_Cor color { get; set; }
            public List<Programa_Cor_Info> Quant { get; set; }
            public List<Programa_Cor_Info_Status> status { get; set; }
        }
    public partial class Quant 
        {
            public int ID_Order { get; set; }
            public int ID_Line { get; set; }
            public int Status{ get; set; }
        }
    List<OrdersColorsViewModel> ResultList = result.ToList();

    sorry if i can't explain myself in the best way, but i'm a new asp and i know that a lot of code i have is not correct, but i'm here to learn,

    Thank you for your help

    Tuesday, February 23, 2021 10:04 PM
  • User475983607 posted

    sorry if i can't explain myself in the best way, but i'm a new asp and i know that a lot of code i have is not correct, but i'm here to learn,

    You are asking a LINQ to Entities question not ASP.NET.  The class structure you've shared is difficult to understand.  Perhaps go through one of the Getting Started tutorials in this site before moving forward.

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/

    Tuesday, February 23, 2021 10:15 PM
  • User1312693872 posted

    Hi,MiguelMi

    What is your DbContext, OrderWithColorsQuantitiesViewModel and other models' structure, it will cause some difficult to reproduce the issue.

    Depend on the sum code you offered, you can try this first:

    var SUM = ResultList.Select(a => a.colors.Select(m => m.Quant.Sum(q => q.Values)));

    Best Regards,

    Jerry Cai

    Wednesday, February 24, 2021 8:25 AM
  • User1670624291 posted

    hi,

    my result

    Jerry Cai

    var SUM = ResultList.Select(a => a.colors.Select(m => m.Quant.Sum(q => q.Values)));

    System.Linq.Enumerable+WhereSelectListIterator`2[Balu0._5.Models.OrdersColorsViewModel,System.Collections.Generic.IEnumerable`1[System.Nullable`1[System.Int32]]]

    Wednesday, February 24, 2021 9:00 AM
  • User1312693872 posted

    Hi,MiguelMi

    What is your ResultList? Set a break point to check whether result1,2 and result can get the right value.

    You didn't offer related models and how you used these code.

    List<OrdersColorsViewModel> ResultList = result.ToList();

    Here the result is OrderWithColorsQuantitiesViewModel while ResultList is OrdersColorsViewModel, they have different types. You should share

    your related code.

    Best Regards,

    Jerry Cai

    Friday, February 26, 2021 9:22 AM
  • User1670624291 posted

    I'll post this as closed and open a new one with raw code, because I have made some changes for better perception, but it gets confusing
    Thanks a lot for the help

    Friday, February 26, 2021 5:38 PM