entity-framework - 从 EF 结果集中过滤现有条目
问题描述
我有一张现有客户及其客户 ID 的表格。当我向表中添加新客户时,我使用自动完成功能,但我想从结果集中排除现有客户。
这就是我到目前为止所拥有的:
public JsonResult AutoCompleteCustomerNumberLimit([FromBody] object value)
{
var input = new
{
value = ""
};
var existingEntries = luxWebSamContext.Limits.Select(u => u.PkKdnr).ToList();
var company = JsonConvert.DeserializeAnonymousType(value.ToString(), input);
var isNumeric = int.TryParse(company.value, out _);
var result = (from c in AlContext.AutolineAccts
where EF.Functions.Like(
isNumeric ? c.AccountCode : c.CustomerSname, "%" + company.value + "%")
&& !existingEntries.Contains(c.AccountCode)
select new
{
CompanyName = c.CustomerSname.Trim(),
CompanyNumber = c.AccountCode.Trim(),
CompanyStreet = c.AddressLine1.Trim(),
CompanyZip = c.Postcode.Trim(),
CompanyCity = c.AddressLine4.Trim(),
CompanyCountry = c.Eccode.Trim()
}).Distinct()
.Take(10);
return Json(result);
}
很容易跟上。existingEntries
包含我想要从结果集中排除的现有客户的 ID 列表。company
保存我的自动完成输入字段中的部分 ID 或名称。
问题是:这种代码正在我的应用程序的另一个功能中使用少量现有实体。但是在这里我在 30 秒后超时。等待查询结束。我在现有条目列表中有 11.000 行,这对于这种查询来说可能太多了。
这里有什么想法吗?
我试图将工作量分成两个步骤:
首先,我检索包括现有客户在内的结果:
var result = (from c in AlContext.AutolineAccts
where EF.Functions.Like(
isNumeric ? c.AccountCode : c.CustomerSname, "%" + company.value + "%")
select new
{
CompanyName = c.CustomerSname.Trim(),
CompanyNumber = c.AccountCode.Trim(),
CompanyStreet = c.AddressLine1.Trim(),
CompanyZip = c.Postcode.Trim(),
CompanyCity = c.AddressLine4.Trim(),
CompanyCountry = c.Eccode.Trim()
}).Distinct()
.Take(10);
然后我从结果中删除现有客户......类似的东西,但我的语法不正确:
result = result.Where(x => x.CompanyNumber != existingEntries.);
解决方案
推荐阅读
- r - 批量下载数据并保存到文件夹
- php - PHP setcookie 函数不支持 httponly 或安全参数
- excel - 在 VBA 中为粘贴的数据指定文件名...显示哪个文件数据来自
- html - css/Boostrap 4 - 视口居中
- amazon-ec2 - “错误:/run/airflow 不存在。无法创建 pidfile。” 将 systemd 用于 Airflow 网络服务器时
- r - 为什么在 rollapply 窗口中将值转换为字符串?
- azure - 如何为文档生成序列号?
- php - 获取 3 个模型之间的相关记录
- python-3.x - 如何从 PDF 嵌入 HTML 的 URL 下载 PDF 文件?
- javascript - Selenium Webdriver Javascript - 使用 sendKeys 键入法语