好的,这是一个棘手的问题。以下是您在 SQL 中的操作方法,并且相对容易:
SELECT *
FROM Projects p
JOIN Timestamps t ON p.Guid = t.GuidProject
WHERE p.Finshed = FALSE AND
t.END = (
SELECT max(t1.END)
FROM Projects p1
JOIN Timestamps t1 ON p1.Guid = t1.GuidProject
WHERE p.Finshed = FALSE AND p.Guid = p1.Guid
)
将其转换为 Linq 会比较棘手。这是我尝试过的,但我无法测试它。
var baseQuery = _contextProvider.Context.Projects
.Join(_contextProvider.Context.Timestamps, p => p.Guid, t => t.GuidProject, (p, t) => new {p, t})
.Where(o => o.p.Finished == false);
var result = baseQuery.Select
.Where(o => o.t.End == baseQuery
.Where(o2 => o2.p == o.p)
.Select(o2 => o2.t.End)
.Max()
).Select( o => new {o.p.Name, o.t.End} );
可能是我把它复杂了一点,它可以用 来实现group by
,试试这样的:
var result = (from p in _contextProvider.Context.Projects
join t in _contextProvider.Context.Timestamps on p.Guid equals t.GuidProject
where p.Finished == false
group t.End by p.Name into g
select new { name=g.Key, end=g.Max()} );