我正在尝试使用Linq的查询获得结果。我使用Mongo驱动程序版本2.1.1,我收到此错误:“$ project或$ group不支持Sum([Cost])”:
lstSavings = (from data in collection.AsQueryable<SavingsDTOMongo>()
where data.ProjectId.Equals(projectId) &
//((projectConsumptionId != default(Guid)) ? data.ProjectConsumptionId == projectConsumptionId : true) &
plantIds.Contains(data.PlantId) &
data.DateTime >= startDate & data.DateTime <= endDate
group data by new { projectId = data.ProjectId, plantId = data.PlantId, cost = data.Cost } into groupedData
select new SavingsDTO
{
ProjectId = groupedData.Key.projectId,
PlantId = groupedData.Key.plantId,
BaselineConsumption = groupedData.Sum(x => x.BaselineConsumption),
BaselineCost = groupedData.Sum(x => x.BaselineCost),
Consumption = groupedData.Sum(x => x.Consumption),
Cost = groupedData.Sum (x => x.Cost)
ConsumptionSavings = groupedData.Sum(x => x.ConsumptionSavings),
CostSavings = groupedData.Sum(x => x.CostSavings)
}).ToList();
驱动程序不允许我选择不同字段的总和,如BaselineCost,消费,.... 只有当我的条件为“plantIds.Contains(data.PlantId)时才会发生。如果我删除了这个条件,它就会起作用。
我找到了解决方案。使用Lambda Expression对数据进行分组。但我想用LinQ对数据进行分组,因为聚合操作直接在mongoServer中执行。
有人知道如何解决这个驱动程序错误,允许select with contains condition和sum(results)来自分组数据?