我有这样的 ef 类:
class Product
{
public int ProductId {set; get;}
...
public List<ProductBannedIn> BannedIn;
}
public class ProductBannedIn
{
public int ProductId {set; get;}
public Product Product {set; get;}
public int CountryId {set; get;}
public Country Country {set; get;}
}
并想提出如下请求:
... //query - added some filters before
var products = query.Include(x => x.BannedIn)
.Join(context.ProductTranslation
.Where(x => x.LanguageId == language.LanguageId),
product => product.ProductId,
translation => translation.Product.ProductId,
(x,y) => new {
Id = x. ProductId,
Name = y.Name,
Description = y.Description,
Type = x.TypeId,
BannedIn = x.BannedIn.Select(b => b.CountryId),
}).ToList();
问题:问题是当我获取例如 1000 件产品时
BannedIn = x.BannedIn.Select(b => b.CountryId)
对每一行进行查询,而且速度很慢。我在调试器中看到的,对于每个产品进行查询以获取 BannedIn,但应该已经获取,因为我有 Include
需要实现的目标:
通常应该像 1 个查询 db 而不是每行 (x.BannedIn)
跃然一笑
元芳怎么了
相关分类