【问题标题】:Lambda Linq Iqueryable group - add another groupingLambda Linq Iqueryable 组 - 添加另一个分组
【发布时间】:2015-03-03 17:25:43
【问题描述】:

我有一种方法用于生成汇总报告,该方法基于用户在选择列表中选择的选项,以计算在某个日期内完成的区域内包含子集位置的某类测试的数量范围。

数据如下所示: 按位置完成的测试计数 从 2015 年 2 月 1 日到 2015 年 3 月 1 日

Total  TestType  Location  Region  Division
455    24 Hour   Lab 1     City 1  Division A
28     24 Hour   Lab 2     City 1  Division A
95     24 Hour   Clinic Z  City 2  Division A
189    24 Hour   Clinic Y  City 2  Division A

以下是不同测试类型在同一时间段的情况:

Total  TestType  Location  Region  Division
285    48 Hour   Lab 1     City 1  Division A
12     48 Hour   Lab 2     City 1  Division A
75     48 Hour   Clinic Z  City 2  Division A
106    48 Hour   Clinic Y  City 2  Division A

现在,用户希望在一个报告中查看testType 的摘要细分(在 SQL 中,将另一个属性添加到分组依据)。在我的testTypeId 选择列表中,我使用 0 作为我的“全部”项目。在一个完美的世界中,如果testTypeId == 0,我会在IQueryable 中添加另一个内容,这样我就可以保留相同的私有方法,而不是编写新的查询。

这是我们希望数据在“ALL”情况下的样子:

Total  TestType  Location  Region  Division
455    24 Hour   Lab 1     City 1  Division A
285    48 Hour   Lab 1     City 1  Division A
59     12 Lead   Lab 1     City 1  Division A
28     24 Hour   Lab 2     City 1  Division A
12     48 Hour   Lab 2     City 1  Division A
95     24 Hour   Clinic Z  City 2  Division A
75     48 Hour   Clinic Z  City 2  Division A
5      12 Lead   Clinic Z  City 2  Division A
189    24 Hour   Clinic Y  City 2  Division A
106    48 Hour   Clinic Y  City 2  Division A
8      12 Lead   Clinic Y  City 2  Division A

下面的示例显示了我想要添加的内容,但语法告诉我我不能将包含IGroupingIQueryable 转换为目标类型@987654330 @。

谁能在这里指出一个好的方向?

private CompletedCountReport GetCompletedCountsByRegion(int regionId, DateTime? startDate, DateTime? endDate, int testTypeId)
{
    var ccRpt = new CompletedCountReport { CompletedCounts = new List<CompletedCount>(), StartDate = startDate, EndDate = endDate, SelectedRegionID = regionId};

    var query = HolterTestDao.FindAll(new GetCompletedByRegionIdAndDates(regionId, startDate.Value, endDate.Value));

    // a specific test type was selected
    if (testTypeId > 0)
    {
        query = query.Where(x => x.HolterTestType == testTypeId);
    }
    // THIS IS WHAT I WANT TO ADD -> otherwise group by test type  <-  THIS IS WHAT I WANT TO ADD.
    if (testTypeId == 0)
    {
        query = query.GroupBy(x => x.HolterTestType);
    }
    // order by locationID within the region.
    query = query.OrderBy(x => x.Location.ID);

    var htList = query.ToList();

    // now Group by Location, to get counts. 
    var reportContents = htList.GroupBy(x => x.Location.ID);
    foreach (var r in reportContents)
    {
        var rList = r.ToList();

        var cc = new CompletedCount();
        var loc = LocationDao.FindById(r.Key);
        cc.Description = loc.Description;
        cc.RegionId = loc.Region.ID;
        cc.DivisionId = loc.Region.Division.ID;
        cc.TestTypeId = testTypeId;
        cc.Count = rList.Count;
        ccRpt.CompletedCounts.Add(cc);
    }

    return ccRpt;
}

【问题讨论】:

  • 为什么要对数据进行有条件的分组?只需无条件地对数据进行分组。
  • 所以你是说总是按位置和测试类型分组?有时他们只想查看一种测试类型的数字,有时他们想查看所有测试类型
  • @JenniferS 您仍然可以有条件地过滤数据,只是不要有条件地对数据进行分组。总是分组,然后有选择地过滤(或相反)。
  • 我不明白。当您的“所有”结果是按位置分组的多个 testType 时,按 testType 分组有什么意义?您不妨完全跳过按测试类型分组。
  • 我对您的问题感到困惑,因为您的“所有”数据示例根本没有按 testtype 分组(它只是按位置排序/分组),我认为这是您试图但无法做到的做。如果您的目标只是获得一个列表,其中包含按位置分组的所有数据以及按 testtype 分组的每个位置,那么我认为您可以在 IQueryable 上简单地使用 OrderBy(x =&gt; x.Location.ID).ThenBy(x =&gt; x.HolterTestType) 并非常简单地使用您的代码。

标签: c# linq lambda iqueryable linq-group


【解决方案1】:

我的解决方案是 OrderBy Location 和 HolterTestType,然后是 GroupBy location 作为基本内容,然后在我的循环中添加另一个分组通过结果来获取测试类型的计数:

        query = query.OrderBy(x => x.Location.ID).ThenBy(x => x.HolterTestType);
        var htList = query.ToList();

        // now Group by Location. 
        var reportContents = htList.GroupBy(x => x.Location.ID);

        foreach (var r in reportContents)
        {
            //Group by TestType, to get counts.
            var t = r.GroupBy(x => x.HolterTestType);
            var tList = t.ToList();

            foreach(var u in tList)
            {
                var cc = new CompletedCount();
                var loc = LocationDao.FindById(r.Key);
                cc.Description = loc.Description;
                cc.RegionId = loc.Region.ID;
                cc.DivisionId = loc.Region.Division.ID;
                cc.TestTypeId = u.Key;
                cc.Count = u.Count();
                ccRpt.CompletedCounts.Add(cc);
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 2020-07-02
    相关资源
    最近更新 更多