我在一个表中有文件,在另一个表中有文件操作。我想获取 Jeff 对其执行操作的最新文件,该文件的状态为 4。在SQL中,它看起来像这样:
SELECT TOP 1 files.*
FROM files
LEFT OUTER JOIN fileActions ON files.FileId = fileActions.FileId
WHERE fileActions.ActionTaker = 'Jeff'
AND files.FileStatus = 4
ORDER BY fileActions.ActionDate DESC
我将如何在 Linq 中表达相同的查询?
我已经尝试了很多东西,但我什至无法编译它。我觉得这是我迄今为止最接近的一次,但显然"top"不是一个 linq 语法词,所以我有一个很好的方法:
var file = from f in context.Files
join fa in context.FileActions on f.FileId equals fa.FileId
where fa.ActionTaker == "Jeff"
where f.FileStatus == 4
orderby fa.ActionDate descending
select top 1 f;
任何帮助,不胜感激。
如果我理解你的问题...
var file = (from f in context.Files
join fa in context.FileActions on f.FileId equals fa.FileId
where fa.ActionTaker == "Jeff" && f.FileStatus == 4
orderby fa.ActionDate descending
select new { Value1 = fa.Id, Value2 = f.Id }).FirstOrDefault();
试试这个:
var file = (from f in context.Files
join fa in context.FileActions on f.FileId equals fa.FileId
where fa.ActionTaker == "Jeff" && f.FileStatus == 4
orderby fa.ActionDate descending
select f).FirstOrDefault();
试试这个
var file = (from f in context.Files
join fa in context.FileActions on f.FileId equals fa.FileId
where fa.ActionTaker == "Jeff"
&& f.FileStatus == 4
orderby fa.ActionDate descending
select f).first();