問題描述
我將 EF5
與 MoreLinq
擴展一起使用,在生產(chǎn)中測試我的程序(非常大的數(shù)據(jù)庫)時,我發(fā)現(xiàn)該行:
I am using EF5
with the MoreLinq
extenstion, while testing my program in production (very big database), I found out that the line:
var x = db.TheBigTable.MaxBy(x => x.RecordTime);
需要很長時間(RecordTime
是一個非索引的datetime
)
Takes very long time (RecordTime
is a non-indexed datetime
)
是不是因為MaxBy
總是在客戶端運行(并且首先從數(shù)據(jù)庫中獲取所有記錄)?
Is that because MaxBy
always runs on the client side (and firstly gets ALL records from the database)?
推薦答案
這里是 MaxBy
擴展方法:
Here is the signature of the MaxBy
extension method:
public static TSource MaxBy<TSource, TKey>(this IEnumerable<TSource> source,
Func<TSource, TKey> selector)
{
return source.MaxBy(selector, Comparer<TKey>.Default);
}
它返回 IEnumerable
的最大元素(基于給定的投影),而不是 IQueryable
.所以查詢db.TheBigTable
的結(jié)果確實都是先加載到內(nèi)存中,然后迭代求最大值.
It returns the maximal element (based on the given projection) of an IEnumerable<T>
, not an IQueryable<T>
. So the results of the query db.TheBigTable
are indeed all loaded into memory first, and then they are iterated to find the maximum.
這篇關(guān)于MoreLinq maxBy vs LINQ max + where的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!