MoreLinq maxBy vs LINQ max + where(MoreLinq maxBy vs LINQ max + where)
问题描述
我将 EF5
与 MoreLinq
扩展一起使用,在生产中测试我的程序(非常大的数据库)时,我发现该行:
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
总是在客户端运行(并且首先从数据库中获取所有记录)?
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
的结果确实都是先加载到内存中,然后迭代求最大值.
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.
这篇关于MoreLinq maxBy vs LINQ max + where的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MoreLinq maxBy vs LINQ max + where


基础教程推荐
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01