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?
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.
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
publicActionResultIndex(){var orders = db.Orders.ToList();var colors = db.Colors.ToList();var quantities = db.Quantities.ToList();//Query syntaxvar result1 =(from o in orders
join c in colors
on o.ID_Orders equals c.ID_Orders into co
groupnew{ o, co }by o.ID_Orders into g
from i in g
selectnew{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
groupnewColorsWithQuantitiesViewModel{ coler=c, quantities=q.ToList()}by c.ID_Orders).ToList();varresult=(from r1 in result1
join r2 in result2
on r1.order.ID_Orders equals r2.KeyselectnewOrderWithColorsQuantitiesViewModel{
order=r1.order,
colers=r2.ToList()}).ToList();returnView(result);}
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
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
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.
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; }
}
publicpartialclass Quant {publicint ID_Order {get;set;}publicint ID_Line {get;set;}public int Status{get;set;}}
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.
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
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
Here the result is OrderWithColorsQuantitiesViewModel while ResultList is OrdersColorsViewModel, they have different types. You should share
your related code.
Best Regards,
Jerry Cai
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
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
Member
22 Points
66 Posts
Question about item count in a result
Feb 17, 2021 04:59 PM|MiguelMi|LINK
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,
All-Star
53711 Points
24042 Posts
Re: Question about item count in a result
Feb 17, 2021 07:30 PM|mgebhard|LINK
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.
If you need the count within the View then "results" becomes the strongly typed view model. The Razor syntax is shown below.
Member
22 Points
66 Posts
Re: Question about item count in a result
Feb 17, 2021 09:43 PM|MiguelMi|LINK
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
Member
22 Points
66 Posts
Re: Question about item count in a result
Feb 18, 2021 05:26 PM|MiguelMi|LINK
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
All-Star
53711 Points
24042 Posts
Re: Question about item count in a result
Feb 18, 2021 07:44 PM|mgebhard|LINK
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...
Member
22 Points
66 Posts
Re: Question about item count in a result
Feb 18, 2021 09:05 PM|MiguelMi|LINK
yes, you're right i'm sorry and my example is complex
my model from table Quantities
Controller
view
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
Participant
1110 Points
371 Posts
Re: Question about item count in a result
Feb 19, 2021 03:36 AM|Jerry Cai|LINK
Hi,MiguelMi
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
Member
22 Points
66 Posts
Re: Question about item count in a result
Feb 23, 2021 07:55 PM|MiguelMi|LINK
I have a problem with my SUM () it is only sum the first value of List to listing in a foreach
my example:
are there any other ways to declare a SUM?
I tried but I have the same result;
thanks,
All-Star
53711 Points
24042 Posts
Re: Question about item count in a result
Feb 23, 2021 09:29 PM|mgebhard|LINK
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.
Member
22 Points
66 Posts
Re: Question about item count in a result
Feb 23, 2021 10:04 PM|MiguelMi|LINK
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
All-Star
53711 Points
24042 Posts
Re: Question about item count in a result
Feb 23, 2021 10:15 PM|mgebhard|LINK
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/
Participant
1110 Points
371 Posts
Re: Question about item count in a result
Feb 24, 2021 08:25 AM|Jerry Cai|LINK
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:
Best Regards,
Jerry Cai
Member
22 Points
66 Posts
Re: Question about item count in a result
Feb 24, 2021 09:00 AM|MiguelMi|LINK
hi,
my result
System.Linq.Enumerable+WhereSelectListIterator`2[Balu0._5.Models.OrdersColorsViewModel,System.Collections.Generic.IEnumerable`1[System.Nullable`1[System.Int32]]]
Participant
1110 Points
371 Posts
Re: Question about item count in a result
Feb 26, 2021 09:22 AM|Jerry Cai|LINK
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.
Here the result is OrderWithColorsQuantitiesViewModel while ResultList is OrdersColorsViewModel, they have different types. You should share
your related code.
Best Regards,
Jerry Cai
Member
22 Points
66 Posts
Re: Question about item count in a result
Feb 26, 2021 05:38 PM|MiguelMi|LINK
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