【发布时间】:2019-05-14 08:46:03
【问题描述】:
大家好,我正在尝试按年龄统计每个男性和女性的人数,如表中here
我能够计算所有年龄,但我无法计算男孩和女孩的年龄。我需要帮助,请
这是我在控制器中的代码
public ActionResult AllCuont()
{
var query = (from t in db.Pations
let range = (
t.Age>= 1 && t.Age < 5 ? "age from 1 to 4" :
t.Age >= 5 && t.Age < 15 ? "age from 5 to 14" :
t.Age >= 15 && t.Age < 25 ? "age from 15 to 24" :
t.Age >= 25 && t.Age < 45 ? "age from 25 to 44" :
""
)
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(db.Pations);
}
和我的模态一样
namespace app.Models
{
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 ApplicationUser User { get; set; }
}
public enum Sex
{
boys,
girls,
}
}
这是计算我的价值的模式
public class UserRange
{
public string AgeRange { get; set; }
public int Count { get; set; }
}
如何按年龄计算每个男性和女性
【问题讨论】:
-
添加db.Pations表数据
-
我如何添加数据库
-
他的意思是:编辑你的问题并向我们展示课程
Pations -
您不能单独按范围分组,而是按范围和性别分组。您无法从特定组中提取性别数据。
-
是的,我的错我添加了我的 db.Pation
标签: asp.net-mvc entity-framework linq asp.net-mvc-4 asp.net-mvc-5.2