MoreLinq maxBy vs LINQ max + where

Ofi*_*ris 5 c# entity-framework linq-to-sql morelinq

我使用EF5MoreLinqextenstion,而在生产(非常大的数据库)测试我的节目,我发现了一行:

var x = db.TheBigTable.MaxBy(x => x.RecordTime);
Run Code Online (Sandbox Code Playgroud)

需要很长时间(RecordTime是非索引的datetime)

这是因为MaxBy总是在客户端运行(并首先从数据库中获取所有记录)?

dtb*_*dtb 8

下面是签名MaxBy扩展方法:

public static TSource MaxBy<TSource, TKey>(this IEnumerable<TSource> source,
    Func<TSource, TKey> selector)
{
    return source.MaxBy(selector, Comparer<TKey>.Default);
}
Run Code Online (Sandbox Code Playgroud)

它返回a的最大元素(基于给定的投影)IEnumerable<T>,而不是a IQueryable<T>.所以查询的结果db.TheBigTable确实首先全部加载到内存中,然后迭代它们以找到最大值.