GroupBy()获取重复记录

时间:2017-09-22 13:31:03

标签: c# linq group-by asp.net-mvc-5

假设我有一个名为TestRecords的数据库表,模型如下所示:

public class TestRecords
{
    public int Id {get;set;}
    public Date DateEntered {get;set;}
    public int SecId {get;set;}
    public int PhoneCallsTaken {get;set;}   
}

现在,我有一个主视图模型,如下所示:

public class MyMainViewModel
{
    public string Location {get;set;}
    public IEnumerable<MyChildViewModel> Children {get;set;}    
}

我的儿童观模型:

public class MyChildViewModel
{
    public string Month { get; set; }
    public string Year { get; set; }
    public double TotalPhoneCalls { get; set; }
}

假设在数据库中有TestRecords表中的记录。

TestRecords

 ID    |    DateEntered    |    SecId    |    PhoneCallsTaken
-------------------------------------------------------------
  1          8/1/2017             2                 35
  2          8/2/2017             2                 30
  5          9/1/2017             2                 30

我想要的结果是什么:

我想按SecId对这些记录进行分组,我知道这只是db.TestRecords.GroupBy(x => x.SecId) ..这不是我的困惑。

以下是SecId等于2

的结果
2

Month    |    Year    |    PhoneCallsTaken
------------------------------------------
 August        2017              65
 September     2017              30

以下是我收到的内容:

2

Month    |    Year    |    Total Phone Calls
------------------------------------------
 August        2017              65
 August        2017              65
 September     2017              30

重复8月份总计,因为数据库中有2条记录SecId等于2且月份是8月。

这是我的C#代码:

var data = db.TestRecords.ToList();

IEnumerable<MyMainViewModel> model = data.GroupBy(x => x.Section.SectionName)
                                         .Select(y => new MyMainViewModel()
{
    Location = y.Key,
    Children = y.Select(z => new MyChildViewModel()
    {
        Month = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(z.DateEntered.Month),
        Year = z.DateEntered.Year.ToString(),
        TotalPhoneCalls = y.Where(t => t.Section.SectionName == z.Section.SectionName)
                           .Sum(t => t.PhoneCallsTaken)
    }).Distinct()
});

如何获得在同一月/年输入的记录不同?

感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

以下是我在评论中提到的基本要点。

var model = data.GroupBy(_ => _.Section.SectionName)
    .Select(section => new MyMainViewModel {
        Location = section.Key,
        Children = section.GroupBy(_ => new {
            Month = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(_.DateEntered.Month),
            Year = _.DateEntered.Year.ToString()
        }).Select(period => new MyChildViewModel {
            Month = period.Key.Month,
            Year = period.Key.Year,
            TotalPhoneCalls = period.Sum(_ => _.PhoneCallsTaken)
        }).ToList()
    });

您最初按部分分组。现在,您还希望按月和年对这些部分进行分组,然后对这些组的调用求和。

答案 1 :(得分:0)

您当前的LINQ:

var data = db.TestRecords.ToList();

IEnumerable<MyMainViewModel> model = data.GroupBy(x => x.Section.SectionName).Select(y => new MyMainViewModel(){  
     Location = y.Key,
     Children = y.Select(z => new MyChildViewModel(){
          Month = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(z.DateEntered.Month),
          Year = z.DateEntered.Year.ToString(),
          TotalPhoneCalls = y.Where(t => t.Section.SectionName == z.Section.SectionName).Sum(t => t.PhoneCallsTaken)
     }).Distinct()
 });

略微调整:

var data = db.TestRecords.Select(d => d.SecId, d.DateEntered, d.PhoneCallsTaken).ToList();

IEnumerable<MyMainViewModel> model = data.GroupBy(x => x.Section.SectionName).Select(y => new MyMainViewModel(){  
     Location = y.Key,
     Children = y.Select(z => new MyChildViewModel(){
          Month = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(z.DateEntered.Month),
          Year = z.DateEntered.Year.ToString(),
          TotalPhoneCalls = y.Where(t => t.Section.SectionName == z.Section.SectionName).Sum(t => t.PhoneCallsTaken)
     }).Distinct()
 });

根据您的需要修改data变量分配,并确保更改反映在稍后使用List中。