嗨,你们好吗?我需要一些帮助,让我先向您解释。我有一个每月有一组患者的表格,这意味着这是一个月的数据编译过程,我想根据疾病类型每月收集患者数据,我的意思是每月总数,取决于我尝试过的疾病类型可以按月收集数据,但是我无法按疾病类型收集数据,在这里我需要您的帮助
我的指像表
{
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,
}
}
疾病模型的类型是我想要分组的pation的外键
{
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
我怎么能这么努力,但是我做不到,请大家帮忙
答案 0 :(得分:0)
我要做的是全部加载并首先执行如下的逻辑。 首先按疾病分组,然后我按月分组,最后按年龄,性别或您需要的分组分组。在我得到结果之后,我会再进行重构,以便它使用linq查询直接从数据库中获取数据:) 所以我将从这样的东西开始
var diseaseGroupByMonth = Pation.GroupBy(x => x.DateWarred.Month);
//might need to set year as well here if you have data for multiple years
var groupDiseaseByMonth = diseaseGroup.GroupBy(x => x.Type_diseaseID)
var group...
现在这样做可能有点乏味,但是我发现首先获取想要的数据然后尝试做得更好会更容易。
编辑:
public DiseaseMonthRange ()
{
public DateTime Month (get; set;)
public List<DiseaseGroup> Group (get; set;)
}
public DiseaseGroup ()
{
public int DiseaseId (get; set;)
public List<UserRange> (get; set;)
}
然后从分组中填充那些模型并显示数据。
希望这会有所帮助