entity-framework-core - 如何在 foreach 循环(EF Core 3)中使用 EF.CompileQuery?
问题描述
我有一个类和一个视图模型类。
UserClass 包含大约 30,000 条记录
[Key]
public int UserId { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string ActiveCode { get; set; }
public bool IsActive { get; set; }
public string UserAvatar { get; set; }
public DateTime RegisterDate { get; set; } = DateTime.Now;
public bool IsDelete { get; set; } = false;
我的视图模型类包含
public string UserName { get; set; }
public string Email { get; set; }
public DateTime RegisterDate { get; set; }
我想像这样运行我的查询
var list= EF.CompileQuery((TopLearnContext db) =>
db.Users.Select(x=>new InformationUserViewModel()
{
Email = x.Email,
UserName = x.UserName,
RegisterDate = x.RegisterDate
}));
foreach (var item in list)
{
}
但在运行时出现此错误:
foreach statement cannot operate on variables of type 'Func<TopLearnContext, IEnumerable<InformationUserViewModel>>' because 'Func<TopLearnContext, IEnumerable<InformationUserViewModel>>' does not contain a public instance definition for 'GetEnumerator'
我的问题是,如何在 EF.CompileQuery 中执行 foreach 循环?
据我所知,EF.CompileQuery 提高了性能,因此我想知道如何在 foreach 中使用它。
解决方案
ACompiledQuery
没有任何东西可以循环。如果您想要查询的结果,则必须先执行它:
var toLoop = list.Invoke(context, ...)
这将是一个IQueryable
可以循环的。
推荐阅读
- django - Django 登录到 wxpython 窗口?
- symfony4 - Api平台错误处理异常?
- dictionary - ansible - 组合三个字典列表
- fortran - 是否可以将 Fortran 派生数据类型指针指向内在数据类型?
- facebook-messenger - 我的 facebook messenger webhook 没有收到未使用与 fb 应用程序相同的帐户创建的页面的消息
- asp.net - 如何保存由 Microsoft.Office.Interop.Excel 生成的文件
- pyspark - 属性错误?Pyspark 数据帧()错误
- sql-server - Transact SQL:具有特定逻辑的有序行内的字符串连接
- c# - 请求失败,HTTP 状态 401:调用 Web 服务时未授权
- ruby - Ruby `cweek` 返回 1 而不是 53