按值对 linq 结果进行分组,对空值或无效值进行分组并进行计数

Group linq results by value and group null or invalid values and do counts

这是 的后续问题。我现在想对分组进行一些计数。

原始查询: 排除了无效的邮政编码并执行了以下操作:

  List<DataSourceRecord> md = (from rst in QBModel.ResultsTable
        where (!String.IsNullOrWhiteSpace(rst.CallerZipCode) && rst.CallerZipCode.Length > 2)
        group rst by rst.CallerZipCode.Substring(0, 3) into newGroup
        orderby newGroup.Key
        select new DataSourceRecord()
        {
          State = newGroup.Select(i => i.CallerState).FirstOrDefault(),
          ZipCode = newGroup.Where(z => z.CallerZipCode.StartsWith(newGroup.Key)).Select(x => x.CallerZipCode.Substring(0, 3)).FirstOrDefault(),
          Calls = newGroup.Where(x => x.CallerZipCode.StartsWith(newGroup.Key) && x.CALL_ID > 0).Distinct().GroupBy(g => new { g.CallerZipCode, g.CTR_ID, g.CALL_ID }).Count(),
          Exposures = newGroup.Where(x => x.CallerZipCode.StartsWith(newGroup.Key) && x.CALL_ID > 0 && x.ExposureCount > 0).Distinct().GroupBy(x => new { x.CallerState, x.CTR_ID, x.CALL_ID }).Count()
        }).ToList();

新示例 1: 现在新分组包括无效的邮政编码分组:

List<DataSourceRecord> newset = (from rst in QBModel.ResultsTable
  group rst by GetGroupRepresentation(rst.CallerZipCode) into newGroup
  select new DataSourceRecord()
  {
    State = newGroup.Select(i => i.CallerState).FirstOrDefault(),
    ZipCode = newGroup.Key,
    Calls = ???
    Exposures = ???
  }).ToList();

分组方式:

private string GetGroupRepresentation(string zipCode)
{
    if (string.IsNullOrEmpty(zipCode) || zipCode.Length < 3)
        return "<null>";
    return zipCode.Substring(0,3);
}

新示例 2: 我认为我还可以执行以下操作:

List<DataSourceRecord> newset = (from rst in QBModel.ResultsTable
  group rst by rst.CallerZipCode == null || rst.CallerZipCode.Trim().Length < 3 ? "<null>" : rst.CallerZipCode.Substring(0, 3) into newGroup
  select new DataSourceRecord()
  {
    State = newGroup.Select(i => i.CallerState).FirstOrDefault(),
    ZipCode = newGroup.Key,
    Calls = ???
    Exposures = ???
  }).ToList();

我想弄清楚我需要在原始查询中为 'Calls' 和 'Exposures' 的两个计数更改新查询中分组的内容。如何以及需要什么来完成这个?

[编辑] 同一问题的扩展:

如何配置两个或多个属性的分组。是贝尔沃

List<DataSourceRecord> 
    newset = (from rst in QBModel.ResultsTable
              group rst by GetGroupRepresentation(rst.CallerZipCode, rst.CallerState) into newGroup
              select new MapDataSourceRecord()
              {
                State = ToTitleCase(newGroup.Select(i => i.CallerState).FirstOrDefault()),
                StateFIPS = FipsForStateCD(newGroup.Select(i => i.CallerStateCD).FirstOrDefault()),
                ZipCode = newGroup.Key[0],
                Calls = newGroup.Where(x => x.CALL_ID > 0).Distinct().Count(),
                Exposures = newGroup.Where(x => x.CALL_ID > 0 && x.EXPO_ID > 0).Distinct().Count(),
                InfoRequests = newGroup.Where(x => x.CALL_ID > 0 && x.INFO_ID > 0).Distinct().Count(),
                Population = GetZipCode3Population(newGroup.Key[0])
              }).ToList();

方法:

    private string[] GetGroupRepresentation(string ZipCode, string State)
    {
      string ZipResult;
      string StateResult;
      if (string.IsNullOrEmpty(ZipCode) || ZipCode.Length < 3)
        ZipResult = "<null>";
      else
        ZipResult = ZipCode.Substring(0, 3);

      if (string.IsNullOrEmpty(State))
        StateResult = "<null>";
      else
        StateResult = State;

      return  new string[]{ ZipResult, State };
    }

首先是电话:

Calls = newGroup.Where(x => x.CallerZipCode.StartsWith(newGroup.Key) && x.CALL_ID > 0).Distinct().GroupBy(g => new { g.CallerZipCode, g.CTR_ID, g.CALL_ID }).Count(),

据我了解,您希望群组的通话次数不同,其中 CALL_ID > 0。我不明白你为什么用邮政编码 CTR_ID 和 CALL_ID 创建一个新组。 如果我没理解错的话,曝光是很相似的。

List<DataSourceRecord> newset = (from rst in QBModel.ResultsTable
  group rst by GetGroupRepresentation(rst.CallerZipCode) into newGroup
  select new DataSourceRecord()
  {
    State = newGroup.Select(i => i.CallerState).FirstOrDefault(),
    ZipCode = newGroup.Key,
    Calls = newGroup.Where(x => x.CALL_ID > 0).Select(x => x.CALL_ID).Distinct().Count(),
    Exposures = newGroup.Where(x => x.CALL_ID > 0 && x.ExposureCount > 0).Distinct().Count()
  }).ToList();

如果您真的想对 calls/exposures 进行分组,这意味着您想要计算 (CTR_ID 和 CALL_ID / CallerState、CTR_ID 和 CALL_ID), 你当然可以这样做。