寻找左外连接的LINQ lambda表达式

时间:2013-06-18 13:38:24

标签: c# sql linq entity-framework

我的报告搜索功能目前如下所示。

public IEnumerable<SendReport> Search(int page, Report filter)
{
    var pageSize = _webConfiguration.PageSize();
    var rowsToSkip = (page - 1)*pageSize;

    var reports = _context.Report
                              .Include(report => report.Status)
                              .Include(report => report.Product);

    reports = ApplyFilters(filter, reports);

    reports = reports.OrderByDescending(report => report.ReportId)
                             .Skip(rowsToSkip)
                             .Take(pageSize);

    return reports;
}

这个表有几十万行,它产生的SQL非常慢,因为它使用INNER JOIN在Status和Product表上执行查找。

我想改变它以使用LEFT OUTER JOIN,任何想法?

如果有帮助,我希望产生这个查询..

SELECT TOP (20) 
r.*, 
s.*,
p.*
FROM   [dbo].[Report] AS r
LEFT OUTER JOIN [dbo].[Status] AS s ON r.[StatusId] = s.[StatusId]
LEFT OUTER JOIN [dbo].[Product] AS p ON r.[ProductId] = p.[ProductId]
ORDER BY r.[ReportId] DESC

而不是这个..

SELECT TOP (20) 
r.*, 
s.*,
p.*
FROM   [dbo].[Report] AS r
INNER JOIN [dbo].[Status] AS s ON r.[StatusId] = s.[StatusId]
INNER JOIN [dbo].[Product] AS p ON r.[ProductId] = p.[ProductId]
ORDER BY r.[ReportId] DESC

0 个答案:

没有答案