我有一个方法,用于根据用户在选择列表中选择的选项生成摘要报告,以计算在某个区域内完成的测试类型的数量,以及日期内的子集位置范围。
以下是数据的样子: 按位置完成测试计数 2015年1月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作为我的“全部”项目。在一个完美的世界中,如果IQueryable
我还有testTypeId == 0
的另一个补充,这样我就可以保留相同的私有方法,而不是编写新的查询。
以下是我们希望数据在“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
下面的示例显示了我想要添加的内容,但语法告诉我,我不能将IQueryable
包含IGrouping
并将其转换为目标类型IQueryable
。
有人能指出我在这方面的好方向吗?
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;
}
答案 0 :(得分:0)
我的解决方案是OrderBy Location和HolterTestType,然后是基本内容的GroupBy位置,然后在我的循环中通过结果添加另一个分组以按测试类型获取计数:
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);
}
}