C# 使用实体框架限制查询大小
这是一个简单的问题(我想),但我还没有找到解决办法。我知道对于其他类型的查询,您可以添加一个限制子句,使查询只返回这么多结果。对于实体查询,这可能吗C# 使用实体框架限制查询大小,c#,sql,entity-framework,C#,Sql,Entity Framework,这是一个简单的问题(我想),但我还没有找到解决办法。我知道对于其他类型的查询,您可以添加一个限制子句,使查询只返回这么多结果。对于实体查询,这可能吗 var productQuery = from b in solutionContext.Version where b.Product.ID != 1 && b.VersionNumber == b.Product.ActiveNumber
var productQuery = from b in solutionContext.Version
where b.Product.ID != 1 && b.VersionNumber == b.Product.ActiveNumber
orderby b.Product.LastNumber
select b;
我只是想让这个查询只返回25个版本对象。谢谢你的帮助。当然。。例如,您可以这样做:
var productQuery = from b in solutionContext.Version
where b.Product.ID != 1 && b.VersionNumber == b.Product.ActiveNumber
orderby b.Product.LastNumber
select b;
var limitedProductQuery = productQuery.Take(25);
对于分页结果,您可能还需要:
var pagedProductQuery = productQuery.Skip(25 * page).Take(25)
您需要的是:
这将在限制内存中的结果之前从表中选择所有内容。David。。不,不是。。它的可查询对象。。它相当于查询。。。当他试图获取一些对象或使用ToList方法等时,它将执行。@David,原始查询中的什么强制执行?@LukaszW.pl,对不起,我没有意识到这一点。
var productQuery = (from b in solutionContext.Version
where b.Product.ID != 1
&& b.VersionNumber == b.Product.ActiveNumber
orderby b.Product.LastNumber
select b).Take(25);
var productQuery = (from b in solutionContext.Version
where b.Product.ID != 1 && b.VersionNumber == b.Product.ActiveNumber
orderby b.Product.LastNumber
select b).Take(25);