我有5张桌子:
NazelShifts
Nazel
罐
PersonnelNazelShifts
移
sql查询是:
SELECT SUM(NazelShift.Eold) AS tEold, SUM(NazelShift.Er) AS tEr, SUM(NazelShift.Ecf) AS tEcf, SUM(NazelShift.Esf) AS tEsf, SUM(NazelShift.ESale) AS tESale, Tank.FuelId,
NazelShift.ShiftId, PersonnelNazelShift.PersonnelId
FROM NazelShift INNER JOIN
Nazel ON NazelShift.NazelId = Nazel.NazelId AND NazelShift.NazelId = Nazel.NazelId INNER JOIN
Tank ON Nazel.TankId = Tank.TankId INNER JOIN
PersonnelNazelShift ON Nazel.NazelId = PersonnelNazelShift.NazelId INNER JOIN
Shift ON NazelShift.ShiftId = Shift.ShiftId AND PersonnelNazelShift.ShiftId = Shift.ShiftId
WHERE (NazelShift.ShiftId = 1)
GROUP BY Tank.FuelId, NazelShift.ShiftId, PersonnelNazelShift.PersonnelId
NazelShift与Nazel和Shift有很多对话 PersonnelNazelShift也与Nazel和Shift有很多关系。
我需要运行此查询:
NazelShifts.Where(i => i.ShiftId == 1)
.GroupBy(i => new
{
i.ShiftId,
i.Nazel.Tank.FuelId,
i.Nazel.PersonnelNazelShifts.SingleOrDefault().PersonnelId
})
.Select(i => new
{
i.Key.ShiftId,
i.Key.PersonnelId,
i.Key.FuelId,
tEold = i.Sum(rr => rr.Eold),
tEr = i.Sum(rr => rr.Er),
tEcf = i.Sum(rr => rr.Ecf),
tEsf = i.Sum(rr => rr.Esf),
tESale = i.Sum(rr => rr.ESale)
})
这在LinqPad4中运行良好,但在vs2012中引发异常:
"方法' Single'和' SingleOrDefault'只能用作 最终查询操作。考虑使用方法' FirstOrDefault'在 相反,这个实例。"
我该如何解决这个问题?