linq需要很长时间才能通过约会

时间:2018-02-22 10:03:03

标签: c# sql linq

我正在尝试将以下sql查询转换为linq。 sql查询需要一秒钟才能运行,但是linq需要大约10秒钟 返回结果。任何人都可以让我知道如何减少linq查询的时间

select datediff(mm, min(QueuedTime), max(QueuedTime)), SequencingQueue 
from tbl_AS_MessageQueue with (nolock)
group by SequencingQueue 
order by datediff(mm, min(QueuedTime), max(QueuedTime)) desc

var longRunningQueries = context.TblMessageQueues.GroupBy(x => x.SequencingQueue).Select(g => new TblMessageQueueDto
{
    DateDiff = DbFunctions.DiffMonths(g.Min(x => x.QueuedTime), 
    g.Max(x => x.QueuedTime)),
    SequencingQueue = g.Key
}).OrderByDescending(a => a.DateDiff).ToList();

1 个答案:

答案 0 :(得分:2)

在订购后重写您的查询以选择DTO:

var longRunningQueries = context.TblMessageQueues
    .GroupBy(x => x.SequencingQueue)
    .Select(g => new {
        DateDiff = DbFunctions.DiffMonths(g.Min(x => x.QueuedTime), g.Max(x => x.QueuedTime)),
        SequencingQueue = g.Key
    }).OrderByDescending(a => a.DateDiff)
    .Select(t => new TblMessageQueueDto {
        DateDiff = t.DateDiff,
        SequencingQueue = t.SequencingQueue
    }).ToList();