Performance 在不使用实体框架的contain方法的情况下比较linq查询中的对象列表
我听说使用contain方法会降低实体框架查询性能 我的类文件:Performance 在不使用实体框架的contain方法的情况下比较linq查询中的对象列表,performance,linq,entity-framework,Performance,Linq,Entity Framework,我听说使用contain方法会降低实体框架查询性能 我的类文件: public partial class VendorInCategory { public int Id { get; set; } public int CategoryId { get; set; } public int VendorId { get; set; } public virtual CategoryMaster CategoryMaster {
public partial class VendorInCategory
{
public int Id { get; set; }
public int CategoryId { get; set; }
public int VendorId { get; set; }
public virtual CategoryMaster CategoryMaster { get; set; }
public virtual UserDetails UserDetails { get; set; }
}
这是我的疑问:
List<int> categoryIds = new List<int>();
for (int i = 0; i < Items.Count(); i++)
{
categoryIds.Add(Convert.ToInt32(Items.ElementAt(i)));
}
var data = context.VendorInCategory
.Where(x => ((categoryIds).Contains(x.CategoryMaster.Id))
{
-------------------
}
有人能告诉我如何在不使用contain关键字的情况下比较此列表吗?您可以尝试以下方法:
var l = from c in context.VendorInCategory
join id in categoryIds on c.CategoryMaster.Id equals id into cid
from id in cid.DefaultIfEmpty()
select new {contextEntry = c, idEntry = id};
var data = (from d in l where d.idEntry != null select d.ContextEntry).ToList();
第一部分连接这两个部分,如果没有找到条目,则将idEntry设置为null。第二部分从CategoryId中存在CategoryMaster.Id的上下文中检索所有VendorInCategory项。我不想以这种形式编写查询。我只想像编写查询一样编写投影查询您的问题用linq标记,这就是问题所在。我不确定这将如何映射到方法。但我的ETL使用相同的格式,它将在0.1秒内平均处理100000多个条目。请检查此链接:请尝试我的查询????