我通过将数据源设置为通过执行LINQ查询获得的IQueryable
对象来填充FormView。基本上它会返回在某个“班次”内持有某个“职位”的员工人数。帮助创建LINQ to SQL查询
int shiftID = 1;
var shiftCount = from x in context.Employees.Take(1)
select new
{
ManagerCount = ((from p in context.Persons
where p.PositionID == 1 && p.ShiftID == shiftID && p.IsEmployee == true
select p.PersonId).Count(),
PartTimeCount = ((from p in context.Persons
where (p.PositionID == 2 || p.PositionID == 3) && p.ShiftID == shiftID && p.IsEmployee == true
select p.PersonId).Count(),
etc, etc...
};
这部分工作正常。然而,当我想要得到的员工数量为所有变化,我不能完全弄清楚如何做到这一点:
//Get all shifts 1, 2, and 3
var shiftCount = from x in context.Employees.Take(1)
select new
{
ManagerCount = ((from p in context.Persons
where p.PositionID == 1 && (p.ShiftID == 1 || p.ShiftID == 2 || p.ShiftID == 3) && p.IsEmployee == true
select p.PersonId).Count()
};
这可能没有什么工作,因为它当然回报的3个值和给出Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
错误。
所以我需要得到返回的三个值的总和(它不总是三个,但取决于位置)。
我已经看过使用Sum和LINQ分组的各种方法,但看起来似乎无法解决这个问题。任何人都可以指引我走向正确的方向吗?
如果你想** **所有的变化,你为什么检查'p.ShiftID'呢? –
嗯,这对我来说很尴尬...... – Marcus