MoreLinq maxBy vs LINQ max + where
- 作者: 小仙女谖恓
- 来源: 51数据库
- 2022-12-08
问题描述
我将 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
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.
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
