Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq to SQL |按日期排列的前5个不同订单_C#_Asp.net_Linq_Sql Server 2008_Sql To Linq Conversion - Fatal编程技术网

C# Linq to SQL |按日期排列的前5个不同订单

C# Linq to SQL |按日期排列的前5个不同订单,c#,asp.net,linq,sql-server-2008,sql-to-linq-conversion,C#,Asp.net,Linq,Sql Server 2008,Sql To Linq Conversion,我有一个SQL查询,我想在asp.net应用程序中从LINQ调用SQL SELECT TOP 5 * FROM (SELECT SongId, DateInserted, ROW_NUMBER() OVER( PARTITION BY SongId ORDER BY DateInserted DESC) rn

我有一个SQL查询,我想在asp.net应用程序中从LINQ调用SQL

SELECT TOP 5 *
FROM   (SELECT SongId,
               DateInserted,
               ROW_NUMBER()
                 OVER(
                   PARTITION BY SongId
                   ORDER BY DateInserted DESC) rn
        FROM   DownloadHistory) t
WHERE  t.rn = 1
ORDER  BY DateInserted DESC 

我不知道通过linq到sql是否可能,如果不可能,请提供其他方法。

我认为您必须通过将sql分区更改为linq组。(实际上,分区所做的就是按歌曲分组,并为每个组选择最新的行。)这样:

IEnumerable<DownloadHistory> top5Results = DownloadHistory
    // group by SongId
    .GroupBy(row => row.SongId)

    // for each group, select the newest row
    .Select(grp => 
        grp.OrderByDescending(historyItem => historyItem.DateInserted)
        .FirstOrDefault()
    )

    // get the newest 5 from the results of the newest-1-per-song partition
    .OrderByDescending(historyItem => historyItem.DateInserted)
    .Take(5);
IEnumerable top5Results=下载历史记录
//按SongId分组
.GroupBy(row=>row.SongId)
//对于每个组,选择最新的行
.选择(grp=>
grp.OrderByDescending(historyItem=>historyItem.DateInserted)
.FirstOrDefault()
)
//从最新的每首歌曲1个分区的结果中获取最新的5个分区
.OrderByDescending(historyItem=>historyItem.DateInserted)
.采取(5);

我认为您必须将SQL分区更改为Linq group by。(实际上,分区所做的就是按歌曲分组,并为每个组选择最新的行。)这样:

IEnumerable<DownloadHistory> top5Results = DownloadHistory
    // group by SongId
    .GroupBy(row => row.SongId)

    // for each group, select the newest row
    .Select(grp => 
        grp.OrderByDescending(historyItem => historyItem.DateInserted)
        .FirstOrDefault()
    )

    // get the newest 5 from the results of the newest-1-per-song partition
    .OrderByDescending(historyItem => historyItem.DateInserted)
    .Take(5);
IEnumerable top5Results=下载历史记录
//按SongId分组
.GroupBy(row=>row.SongId)
//对于每个组,选择最新的行
.选择(grp=>
grp.OrderByDescending(historyItem=>historyItem.DateInserted)
.FirstOrDefault()
)
//从最新的每首歌曲1个分区的结果中获取最新的5个分区
.OrderByDescending(historyItem=>historyItem.DateInserted)
.采取(5);

虽然McGarnagle answer解决了这个问题,但当我看到这两个查询的执行计划时,我非常惊讶地发现linq to sql与本机sql查询相比速度太慢了。请参阅生成的查询以获取上述linq到sql:

--It took 99% of the two execution

SELECT TOP (5) [t3].[SongId], [t3].[DateInserted]
    FROM (
        SELECT [t0].[SongId]
        FROM [dbo].[DownloadHistory] AS [t0]
        GROUP BY [t0].[SongId]
        ) AS [t1]
    OUTER APPLY (
        SELECT TOP (1) [t2].[SongId], [t2].[DateInserted]
        FROM [dbo].[DownloadHistory] AS [t2]
        WHERE [t1].[SongId] = [t2].[SongId]
        ORDER BY [t2].[DateInserted] DESC
        ) AS [t3]
    ORDER BY [t3].[DateInserted] DESC


--It took 1% of the two execution
SELECT TOP 5 t.SongId,t.DateInserted
    FROM   (SELECT SongId,
               DateInserted,
               ROW_NUMBER()
                 OVER(
                   PARTITION BY SongId
                   ORDER BY DateInserted DESC) rn
        FROM   DownloadHistory) t
    WHERE  t.rn = 1
    ORDER  BY DateInserted DESC 

虽然McGarnagle answer解决了这个问题,但当我看到这两个查询的执行计划时,我非常惊讶地发现linq to sql与本机sql查询相比速度太慢了。请参阅生成的查询以获取上述linq到sql:

--It took 99% of the two execution

SELECT TOP (5) [t3].[SongId], [t3].[DateInserted]
    FROM (
        SELECT [t0].[SongId]
        FROM [dbo].[DownloadHistory] AS [t0]
        GROUP BY [t0].[SongId]
        ) AS [t1]
    OUTER APPLY (
        SELECT TOP (1) [t2].[SongId], [t2].[DateInserted]
        FROM [dbo].[DownloadHistory] AS [t2]
        WHERE [t1].[SongId] = [t2].[SongId]
        ORDER BY [t2].[DateInserted] DESC
        ) AS [t3]
    ORDER BY [t3].[DateInserted] DESC


--It took 1% of the two execution
SELECT TOP 5 t.SongId,t.DateInserted
    FROM   (SELECT SongId,
               DateInserted,
               ROW_NUMBER()
                 OVER(
                   PARTITION BY SongId
                   ORDER BY DateInserted DESC) rn
        FROM   DownloadHistory) t
    WHERE  t.rn = 1
    ORDER  BY DateInserted DESC 

它就像魅力一样。我必须说看到快速的结果真是太棒了。向你致敬McGarnagle!!!看看这里真正的工作实现:它就像魅力一样工作。我必须说看到快速的结果真是太棒了。向你致敬McGarnagle!!!请参见此处的实际工作实现: