Answered by:
How to Calculate Values Between Ages

Question
-
User-1892044535 posted
Hello guys, I want to Collect values between ages like this table and i am using mvc5 code first how can i display count values between the ages from my database
age from
1--4
age from
5--10
age from
11--16
age from
17--22
age from
23--27
sum ? ? ? ? ? The ages i entered by a textbox how can i Sort and collect them
and this is my model
public class Pation
{
[Key]
public int Id { get; set; }
public string PationName { get; set; }
public int Age { get; set; }
public Sex Sex { get; set; }
public Viset viset { get; set; }
public Free IsFree { get; set; }
public string NameProd { get; set; }
public string esalNum { get; set; }
public string Pationphone { get; set; }
public string cons { get; set; }
public DateTime DateWared { get; set; } = DateTime.UtcNow.AddHours(3);}
Sunday, December 2, 2018 2:37 PM
Answers
-
User-271186128 posted
Hi mas,
mas mas
I want to Collect values between ages like this table and i am using mvc5 code first how can i display count values between the ages from my databaseYou could refer to the following code to collect the value.
from u in users let range = (u.Age >= 0 && u.Age < 10 ? "0-9" : u.Age >= 10 && u.Age < 15 ? "10-14" : u.Age >= 15 && u.Age < 50 ? "15-49" : "50+") group u by range into g select new { g.Key, Count=g.Count() };
Best regards,
Dillion- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, December 3, 2018 2:10 AM -
User-271186128 posted
Hi mas,
You could refer to the following code to use Foreach or For statement to display the values.
var query = (from t in datalist let range = ( t.Age>= 0 && t.Age< 10 ? "0-9" : t.Age>= 11 && t.Age< 15 ? "10-14" : t.Age>= 15 && t.Age< 50 ? "15-50" : "50+" ) group t by range into g select new UserRange { AgeRange = g.Key, Count = g.Count() }).ToList();
//add the sum column. query.Add(new UserRange() { AgeRange = "Sum", Count = query.Sum(c => c.Count) }); ViewBag.UserData = query;Code in the view:
<table border="1"> <tr> @foreach (var item in ViewBag.UserData) { <th>@item.AgeRange </th> } </tr> <tr> @foreach (var item in ViewBag.UserData) { <td>@item.Count </td> } </tr> </table>
The result as below:
Best regards,
Dillion- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, December 4, 2018 2:13 AM -
User-271186128 posted
Hi mas,
mas mas
the error says cannot convert from 'int' to 'system.collections.generic.ienumerableIn my code, the Count property should be the int type, instead of the IEnumerable type, like this:
public class UserRange { public string AgeRange { get; set; } public int Count { get; set; } }
Best regards,
Dillion- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, December 7, 2018 1:31 AM
All replies
-
User-271186128 posted
Hi mas,
mas mas
I want to Collect values between ages like this table and i am using mvc5 code first how can i display count values between the ages from my databaseYou could refer to the following code to collect the value.
from u in users let range = (u.Age >= 0 && u.Age < 10 ? "0-9" : u.Age >= 10 && u.Age < 15 ? "10-14" : u.Age >= 15 && u.Age < 50 ? "15-49" : "50+") group u by range into g select new { g.Key, Count=g.Count() };
Best regards,
Dillion- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, December 3, 2018 2:10 AM -
User-1892044535 posted
I Really wont thank you so thank you bro so much but how my view it's going to be like ? just give me one example i am sorry
Monday, December 3, 2018 11:29 AM -
User-1892044535 posted
Please man please help me manMonday, December 3, 2018 6:43 PM -
User-271186128 posted
Hi mas,
You could refer to the following code to use Foreach or For statement to display the values.
var query = (from t in datalist let range = ( t.Age>= 0 && t.Age< 10 ? "0-9" : t.Age>= 11 && t.Age< 15 ? "10-14" : t.Age>= 15 && t.Age< 50 ? "15-50" : "50+" ) group t by range into g select new UserRange { AgeRange = g.Key, Count = g.Count() }).ToList();
//add the sum column. query.Add(new UserRange() { AgeRange = "Sum", Count = query.Sum(c => c.Count) }); ViewBag.UserData = query;Code in the view:
<table border="1"> <tr> @foreach (var item in ViewBag.UserData) { <th>@item.AgeRange </th> } </tr> <tr> @foreach (var item in ViewBag.UserData) { <td>@item.Count </td> } </tr> </table>
The result as below:
Best regards,
Dillion- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, December 4, 2018 2:13 AM -
User-1892044535 posted
bro how can i thank you bro man you save my live men thank you so much
Tuesday, December 4, 2018 10:21 PM -
User-1892044535 posted
hi Zhi Lv how you doing Zhi Lv i try the add to code but i got some error bro
public ActionResult AllCuont()
{
var query = (from t in db.Pations
let range = (
t.Age >= 0 && t.Age < 10 ? "0-9" :
t.Age >= 11 && t.Age < 15 ? "10-14" :
t.Age >= 15 && t.Age < 50 ? "15-50" :
"50+"
)
group t by range into g
select new UserRange { AgeRange = g.Key, Count = g.Count() }).ToList();
//add the sum column.
query.Add(new UserRange() { AgeRange = "Sum", Count = query.Sum(c => c.Count) });
ViewBag.UserData = query;
return View();
}
and I added model like thisusing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Archive.Models
{
public class UserRange
{
public string AgeRange { get; set; }
public IEnumerable<Pation> Count { get; set; }
}
}
the error says cannot convert from 'int' to 'system.collections.generic.ienumerable
Thursday, December 6, 2018 3:58 PM -
User-1892044535 posted
hi Zhi Lv how you doing Zhi Lv i try the add to code but i got some error bro
public ActionResult AllCuont()
{
var query = (from t in db.Pations
let range = (
t.Age >= 0 && t.Age < 10 ? "0-9" :
t.Age >= 11 && t.Age < 15 ? "10-14" :
t.Age >= 15 && t.Age < 50 ? "15-50" :
"50+"
)
group t by range into g
select new UserRange { AgeRange = g.Key, Count = g.Count() }).ToList();
//add the sum column.
query.Add(new UserRange() { AgeRange = "Sum", Count = query.Sum(c => c.Count) });ViewBag.UserData = query;
return View();
}and I added model like this
{
public class UserRange
{
public string AgeRange { get; set; }
public IEnumerable<Pation> Count { get; set; }
}
}the error says cannot convert from 'int' to 'system.collections.generic.ienumerable
idon't know why this happen't
Thursday, December 6, 2018 4:01 PM -
User-271186128 posted
Hi mas,
mas mas
the error says cannot convert from 'int' to 'system.collections.generic.ienumerableIn my code, the Count property should be the int type, instead of the IEnumerable type, like this:
public class UserRange { public string AgeRange { get; set; } public int Count { get; set; } }
Best regards,
Dillion- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, December 7, 2018 1:31 AM -
User-1892044535 posted
Thank you, my friend. I worked. Thank you very much, brother. I appreciate you standing with me brother
Friday, December 7, 2018 9:38 AM