【发布时间】:2020-06-21 10:38:23
【问题描述】:
我有一个 DbSet 类:
public class Manufacturer
{
public Guid Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public virtual Category Category { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
我知道我可以使用Skip() 和Take() 来限制manufacturers。但我的要求是在所有manufacturers 中获得有限的Products。我正在使用类似的东西,但它不起作用
var manufacturers = await _context.Manufacturers.Where(x => x.Products.Take(10))
.ToListAsync();
PS:我正在使用延迟加载(不是急切加载)
编译错误是:
不能隐式转换类型 'System.Collections.Generic.IEnumerable
' 到 'bool' 无法将 lambda 表达式转换为预期的委托类型,因为 块中的某些返回类型不可隐式转换 到委托返回类型
我怎样才能获得所有制造商但有限的产品?
【问题讨论】:
-
什么不起作用?也许你忘了使用
Include? -
我正在使用 LazyLoading
-
@PavelAnikhouski 用编译器错误更新了问题
-
您的具体要求是什么?只获得 10 种产品的制造商? 10 是多少?
-
我想你想要像
await _context.Manufacturers.Select(m => { m.Products = m.Products.Take(10); return m; }).ToListAsync();这样的东西,但我怀疑它在 EF 中可能是这样的。
标签: c# asp.net-core entity-framework-core