在Mvc5中使用EntityFramwork按内部分组

时间:2018-12-13 07:10:26

标签: asp.net-mvc entity-framework linq asp.net-mvc-4 asp.net-mvc-5.2

伙计们,我正在尝试按年龄计算男性和女性的人数,如表cut

我能够计算所有年龄,但我不能将年龄归为男孩和女孩。我需要帮助

这是我在控制器中的代码

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; }

    }

如何按年龄对男性和女性进行计数

1 个答案:

答案 0 :(得分:1)

我不清楚您要在图片行中放入什么。 Pations?那么一列可能比另一列有更多行?您想在最后一栏中输入什么?

无论如何,您有一个Pations(患者?)序列,并且想要将它们分为年龄范围相等的Pations组。每个年龄段相同的人群都应分为Pations相等的Sex组。

因此,让我们首先给出每个Pation和年龄范围。出于效率考虑,我将您的年龄范围从零到四,然后将年龄范围更改为文本

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(),
    })

由于ToList,您将拥有Boys以及Boys的数量。如果您不需要最终结果中的男孩和女孩,而只需要男孩的数量,则将Count替换为Count

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

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

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

相关问题