【问题标题】:GroupBy() getting duplicate recordsGroupBy() 获取重复记录
【发布时间】:2018-03-04 02:38:35
【问题描述】:

假设我有一个名为 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 =&gt; 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()
});

我怎样才能使在同一月份/年份输入的记录不同?

感谢任何帮助。

【问题讨论】:

  • 按具有这两个属性的匿名对象分组。
  • 带有样本输入和预期输出的stackoverflow.com/help/mcve 会很棒。
  • 您需要创建一个临时对象,其中仅包含您想要返回的字段。您当前的设计在执行 GroupBy 时包含对象的所有属性,如果您在 sql 中执行相同的语句,您将收到与您得到的类似结果
  • @Nkosi 你是什么意思?你能提供一个视觉解释吗?

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


【解决方案1】:

您当前的 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 变量分配,并确保更改反映在您以后使用列表中。

【讨论】:

    【解决方案2】:

    这是我在 cmets 中提到的基本要点。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-06
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      • 2020-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多