【发布时间】:2019-06-13 09:48:19
【问题描述】:
嗨,你们好吗?我需要一些帮助让我先向您解释一下。我有一个每月包含一组患者的表格,这意味着这是一个月的数据编译过程我想根据疾病类型每月收集患者数据我的意思是每月患者总数,这取决于我尝试过的疾病类型按月收集数据,但我无法在这里按疾病类型收集数据,我需要你的帮助
我的餐桌是这样的
{
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 DateTime DateWared { get; set; } = DateTime.UtcNow.AddHours(3);
public int Type_diseaseId { get; set; }
[ForeignKey("Type_diseaseId")]
public virtual Type_disease Type_diseases { get; set; }
public ApplicationUser User { get; set; }
}
public enum Sex
{
boys,
girls,
}
}
疾病模型的类型,它是我想要分组的外键,就像这样
{
public class Type_disease
{
[Key]
public int Id { get; set; }
public string Namedisease { get; set; }
public virtual ICollection<Pation> Pations { get; set; }
}
}
我在我调用的模型中按年龄和总和值对 pation 进行分组
UserRing 是这样的
{
public class UserRange
{
public string AgeRange { get; set; }
public int Count { get; set; }
public string Gender { get; internal set; }
public string grilsTotal { get; internal set; }
public string boysTotal { get; internal set; }
public string Type_d { get; internal set; }
}
}
然后我将名为 ShowPation 的 actionResult 添加到像这样按 pation 的结果分组
public ActionResult ShowPation()
{
var query1 = from t in db.Pations()
let Agerange =
(
t.Age >= (0) && t.Age < (1) ? "Under year" :
t.Age >= (1) && t.Age < (5) ? "1 _ 4" :
t.Age >= (5) && t.Age < (15) ? "5 _ 14" :
t.Age >= (15) && t.Age < (25) ? "15 _ 24" :
t.Age >= (25) && t.Age < (45) ? "24 _ 44" :
"over 45"
)
let Sex = (t.Sex == 0 ? "boys" : "girls")
group new { t.Age, t.Sex, Agerange } by new { t.DateWared.Year, t.DateWared.Month, Agerange, Sex } into g
select g;
var query2 = from g in query1
select new { mycount = g.Count(), g.Key.Year, g.Key.Month, g.Key.Agerange, g.Key.Sex };
var query3 = from i in query2
group i by new { i.Year, i.Month} into g
select g;
Dictionary<string, List<UserRange>> urn = new Dictionary<string, List<UserRange>>();
foreach (var item in query3)
{
foreach (var item1 in item)
{
if (!urn.ContainsKey(item.Key.Month + "/" + item.Key.Year))
{
urn[item.Key.Month + "/" + item.Key.Year] = new List<UserRange>();
}
urn[item.Key.Month + "/" + item.Key.Year].Add(new UserRange { Count = item1.mycount, AgeRange = item1.Agerange, Gender = item1.Sex });
}
urn[item.Key.Month + "/" + item.Key.Year] = urn[item.Key.Month + "/" + item.Key.Year].OrderByDescending(i => i.AgeRange).ToList();//sort the data according to Agerange
}
return View(urn);
}
这样的观点
@model Dictionary<string, List<The_Hospital_Project.Models.UserRange>>
<table border="1" class="table table-responsive table-bordered table-hover table-striped " style="height: 145px;text-align: center; border: solid 1px;border-radius: 5px;direction: rtl;">
@foreach (string key in Model.Keys)
{
<tr>
<td colspan="17"> <center>@key</center></td>
</tr>
<tr>
@foreach (The_Hospital_Project.Models.UserRange item1 in Model[key])
{
<td style="height: 57px;">@item1.AgeRange</td>
}
</tr>
<tr>
@foreach (The_Hospital_Project.Models.UserRange item1 in Model[key])
{
<td>@item1.Gender</td>
}
</tr>
<tr>
@foreach (The_Hospital_Project.Models.UserRange item1 in Model[key])
{
<td>@item1.Count</td>
}
</tr>
}
</table>
该表只是每月收集所有记录。 我想要的是按疾病类型对记录进行排序 每个月,到目前为止,代码可以很好地按月收集数据
我想要的是根据疾病模型的类型在每个月内按疾病收集数据
pation 的外键是每个月的图像结果 here
我怎么能做到这一点,我努力了,但我不能做到,请大家帮帮我
【问题讨论】:
标签: c# entity-framework linq model-view-controller asp.net-mvc-5