【发布时间】:2018-12-08 13:33:38
【问题描述】:
(更新:抱歉,我删除了 AsEnumerable() 代码,因为当您使用 AsEnumerable 时,linq to sql 对象执行 sql 查询并将完整的表带到内存中,该表可以有超过 10000000 行,我想做什么是执行 where in 子句) 我正在尝试在 C# 中的 EF (id="EntityFramework" version="6.2.0") 中创建一个“where in”子句,以不将整个表带入内存,但是当使用下面的代码时,select where in 子句确实不工作我知道我使用的 DOCUMENT_ID 存在于表中:
public partial class Table
{
[Key]
[Column(Order = 0)]
public long id { get; set; }
[StringLength(100)]
public string DOCUMENT_ID { get; set; }
public DateTime? xxx { get; set; }
[StringLength(4000)]
public string yyy { get; set; }
public int? zzz { get; set; }
public int? jjj { get; set; }
}
//this is a "patched" code
using (BBDDCon BBDD = new BBDDCon())
{
//this list it is filled with a EF query
idsuptate= new List<String>() {"1","2","3"};
var list_docs_procesed_error_aux = BBDD.Table.Where(d => (d.xxx != null || String.IsNullOrEmpty(d.yyy) || d.jjj != null));
//this one gets me 0 rows but it should bring 300 rows
var list_docs_procesed_error= list_docs_procesed_error_aux.Where(d => idsuptate.Contains( d.DOCUMENT_ID.Trim())).ToList();
}
我找到了一个可行的解决方案,但作为可枚举将整个表带入内存,正如我所说,我不想这样做:
https://forums.asp.net/t/1661185.aspx?Contains+method+not+working+in+Linq+to+Entities
在 'tableName' 之后和应用 'where' 方法之前使用 AsEnumerable。
objDataContext = compareIndiaDataContext;
objCProduct = objDataContext.Products.AsEnumerable().Where(db => alSelectedIDs.Contains(db.Product_BrandID)).ToList<Product>();
return objCProduct;
¿有什么想法吗? ¿ 可能是一个错误? 非常感谢!
【问题讨论】:
-
为什么加
AsEnumerable()?什么“没用”? -
抱歉复制粘贴出错。代码现在更新正常。
-
@Gert Arnold 重点是不要使用 AsEnumerable() 但我试图查看代码测试是否有效。问题是这不是一个有效的响应,因为我想在 sql server 中过滤,而不是在 BL 服务器中过滤,因为内存问题......
-
你已经解决了吗?
-
嗯,是的,EF 中有一个错误,PK 中有空格,但代码一开始就很好..
标签: c# entity-framework where-in