Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 使用Mvc5中的EntityFramwork按组分组_Asp.net Mvc_Entity Framework_Linq_Asp.net Mvc 4_Asp.net Mvc 5.2 - Fatal编程技术网

Asp.net mvc 使用Mvc5中的EntityFramwork按组分组

Asp.net mvc 使用Mvc5中的EntityFramwork按组分组,asp.net-mvc,entity-framework,linq,asp.net-mvc-4,asp.net-mvc-5.2,Asp.net Mvc,Entity Framework,Linq,Asp.net Mvc 4,Asp.net Mvc 5.2,嗨,伙计们,我在试着,我试着按照表中的年龄来计算每个男性和女性的人数 我能计算出所有年龄,但我不能把年龄计算成男孩和女孩。我需要帮助 这是我在控制器中的代码 public ActionResult AllCuont() { var query = (from t in db.Pations let range = ( t.Age>= 1 && t.Age < 5 ?

嗨,伙计们,我在试着,我试着按照表中的年龄来计算每个男性和女性的人数

我能计算出所有年龄,但我不能把年龄计算成男孩和女孩。我需要帮助

这是我在控制器中的代码

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);
}
这是计算我价值的模式


如何计算每个年龄段的男性和女性

我不清楚你想在照片的行中放什么。天井?所以一列可以比另一列多行?你想在最后一篇专栏文章里写些什么

总之,您有一系列患者?,您希望将他们分成年龄范围相同的患者组。每个年龄段相同的群体应划分为性别相同的患者群体

因此,让我们首先给出每个年龄段。为了提高效率,我会将您的年龄范围从0到4进行编号,稍后我会将年龄范围更改为文本

var query = dbContext.Pations                   // from the table of Pations
    .Where(patient => patient.Age < 45)         // keep only the Patiens younger than 45
    .Select(patient => new                      // from every remaining patient,
    {                                           // make one new object
         AgeRange = (patient.Age < 5) ? 0 :
                    (patient.Age < 15) ? 1 :
                    (patient.Age < 25) ? 2 : 3, // with a number 0..3, indicating the age range
         Gender = patient.Sex,                  // a property indicating the gender

         Patient = patient,                     // and the original Patient
    })
因为ToList,你会有男孩和男孩的数量。如果您不需要最终结果中的男孩和女孩,而只需要男孩的数量,请将ToList替换为Count

最后,将所有数据从数据库管理系统移动到本地进程,并将年龄组转换为文本:

    .AsEnumerable()
    .Select(group => new
    {
        Description = AgeRangeToText(group.AgeRange),
        NrOfBoys = group.NrOfBoys,
        NrOfGirls = group.NrOfGirls,
    });

您唯一需要做的就是编写一个函数,将您的年龄范围0..4转换为正确的文本。

添加db.Pations表数据我如何添加db Pations她的意思是:编辑您的问题并向我们显示类别Pations您不能单独按范围分组,而是按范围和性别分组。你没有办法从特定群体中提取性数据。是的,我很糟糕,我添加了我的db.PationHarald Coppoolse谢谢你兄弟的努力,但我很困惑。我是这方面的初学者,但我将如何调用视图中的输出?你能给我一个视图中的示例吗?我将完成其余我正在使用的razorI不知道razor。此外,我已经写了关于你奇怪的表格:你想在每一行中放入什么?@ HalaldCoppOLSE剃须刀让你在C的中间将C对象传递给页面。例如,如果他把一个列表传递给视图,他可以在HTML中间写一个前缀,可以为集合中的每个对象生成一个Li元素。仅供参考-这很酷。
var query = dbContext.Pations                   // from the table of Pations
    .Where(patient => patient.Age < 45)         // keep only the Patiens younger than 45
    .Select(patient => new                      // from every remaining patient,
    {                                           // make one new object
         AgeRange = (patient.Age < 5) ? 0 :
                    (patient.Age < 15) ? 1 :
                    (patient.Age < 25) ? 2 : 3, // with a number 0..3, indicating the age range
         Gender = patient.Sex,                  // a property indicating the gender

         Patient = patient,                     // and the original Patient
    })
    .GroupBy(patient => patient.AgeRange,       // group into groups of equal age range
    (ageRange, patientsWithThisAgeRange) => new // from the common ageRange and all
    {                                           // patients with this ageRange make one new
        AgeRange = ageRange,                   // remember the ageRange

        // put all Boys in this agegroup in a separate subgroup
        BoysGroup = patientsWithThisAgeRange                       
            .Where(patientWithThisAgeRange => patientWithThisAgeRange.Gender == Sex.Boys)
            .ToList(),

         // put all Girls in a separate sub group

        GirlsGroup = patientsWithThisAgeRange    // count number of girls in this group
            .Where(patientWithThisAgeRange => patientWithThisAgeRange.Gender == Sex.Girls)
            .ToList(),
    })
    .AsEnumerable()
    .Select(group => new
    {
        Description = AgeRangeToText(group.AgeRange),
        NrOfBoys = group.NrOfBoys,
        NrOfGirls = group.NrOfGirls,
    });