【问题标题】:Group by Inside Group by Using EntityFramwork in Mvc5在 Mvc 5 中使用实体框架按内部组分组
【发布时间】: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


【解决方案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,您将拥有男孩和男孩的数量。如果最终结果中不需要男孩和女孩,而只需要男孩的数量,请将 ToList 替换为 Count

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

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

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

【讨论】:

  • Harald Coppoolse 感谢兄弟所做的巨大努力,但我很困惑。我是这方面的初学者,但是我将如何调用视图中的输出你能给我一个视图中的例子,我将完成剩下的我使用剃须刀
  • 我不知道剃须刀。此外,我已经写过你奇怪的桌子:你想在每一行放什么?
  • @HaraldCoppoolse Razor 允许您通过将 C# 对象传递给页面来将 C# 放在 HTML 中间。例如,如果他将List&lt;Patient&gt; 传递给视图,他可以在HTML 中间编写一个foreach,它可以为集合中的每个对象生成一个li 元素。仅供参考——这很酷。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多