我在这里有以下查询。如何为此sql获取类似的linq查询。
var express = require("express");
var app = express();
var server = app.listen(3000);
// Respond to 'Ctrl+C'
process.on("SIGINT", function () {
// stop accepting connections
server.close(function () {
// connections are closed
// exit the process
process.exit(0);
});
});
// Server is shutting down
process.on("SIGTERM", function () {
// stop accepting connections
server.close(function () {
// connections are closed
// exit the process
process.exit(0);
});
});
我使用了下面的linq翻译,但它在某处失败了。
SELECT *
FROM PublishedLineBar
WHERE PublishedRosterShiftId
IN (SELECT LatestShiftId FROM
( SELECT MAX(PublishedRosterShiftId) as LatestShiftId, DayNumber
FROM PublishedRosterShift
WHERE employeeid = 14454
GROUP BY DayNumber)
as ShiftProjection )
答案 0 :(得分:1)
以下是用于SQL IN (...)
子句的子查询的LINQ等效项(删除了不必要的嵌套):
var inner = dbContext.PublishedRosterShifts
.Where(s => s.EmployeeId == EmployeeId)
.GroupBy(s => s.DayNumber)
.Select(g => g.Max(s => s.PublishedRosterShiftId));
和使用它的查询:
var query = from l in dbContext.PublishedLineBars
where inner.Contains(l.PublishedRosterShiftId)
select l;
或只是
var query = dbContext.PublishedLineBars
.Where(l => inner.Contains(l.PublishedRosterShiftId));
您在尝试中缺少的是SQL SELECT MAX(PublishedRosterShiftId) as LatestShiftId, DayNumber
对GROUP BY
运算符的结果进行操作,因此在LINQ中Select
应该在GroupBy
之后。