Linq加入并订购

时间:2015-06-17 18:03:32

标签: c# sql linq

我在一个表中有文件,在另一个表中有文件操作。我想获取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;

感谢任何帮助。

3 个答案:

答案 0 :(得分:3)

如果我理解你的问题......

 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();

答案 1 :(得分:1)

试试这个:

<?php
    $connect=mysqli_connect("localhost","root","root","dbname") or      die("Unable to Connect");

    $showtablequery="SHOW TABLES FROM dbname";
    $query_result=mysqli_query($showtablequery);
    while($showtablerow = mysqli_fetch_array($query_result))
    {
    echo $showtablerow[0]." ";
    } 
?>

答案 2 :(得分:1)

试试这个

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();