[英]Get top n records with top m of relative table in Entity Framework
I have a table Categories
and Products
. 我有一张桌子
Categories
和Products
。 One category can have multiple products and I want to get TOP 5 products and TOP 3 categories for each product. 一个类别可以有多个产品,我想为每种产品获得TOP 5产品和TOP 3类别。 I tried
我试过了
entity.Categories.Include("Products").Take(3)
But output of this is 3 Categories and all products under them. 但是此输出为3个类别及其下的所有产品。 I tried
我试过了
entity.Categories.Take(5).Include("Products").Take(3)
But of course it won't work because Include
can't be called in Take
. 但是当然不能用,因为不能在
Take
调用Include
。 So what could be the solution? 那么解决方案是什么? Please suggest.
请提出建议。
Finally I have solved it 终于我解决了
var result = entity.Categories.Select
(
cats => new
{
cats.CategoryName,
cats.Description,
Products = cats.Products.Take(3)
}
).Take(5);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.