简体   繁体   English

asp.net mvc中的分页

[英]pagination in asp.net mvc

I have categories and subcategories. 我有类别和子类别。 Each subcategory has some products. 每个子类别都有一些产品。 eg programming category has | 例如,编程类别有| C# , java , basic subcategories | C#,java,基本子类别| where c# has c# 3 , c# 3.5 products | 其中c#有c#3,c#3.5产品| java has java ee , java me and so on java有java ee,java me等

I want to select 10 products from categories using linq to EF. 我想从linq到EF中选择10个类别的产品。

The problem is that I don't want to load all products from the database and then sort them and then select 10 of them. 问题是我不想从数据库加载所有产品,然后对它们进行排序,然后选择其中的10个。

I want a solution which i can just take 10 products from database without transferring all products from the database back to web server. 我想要一个解决方案,我可以从数据库中获取10个产品,而无需将所有产品从数据库传输回Web服务器。

I know that EF is greedy and would take only 10 products and skip some. 我知道EF很贪婪,只需10件产品就可以了。 but in my case which i have categories and subcategories so I think I should first select all products from different subcategories belonging to a category and append them to a list which cause bringing all products to server and then select 10 of them. 但在我的情况下,我有类别和子类别所以我认为我应该首先选择属于一个类别的不同子类别的所有产品,并将它们附加到一个列表,导致所有产品到服务器,然后选择其中10个。

What is the best practice so that i don't have to transfer all products to server? 什么是最佳做法,以便我不必将所有产品转移到服务器?

You can do this in one query: 您可以在一个查询中执行此操作:

var pagedProducts = _db.Categories.Join( 
    _db.Products,
    c => c.CategoryId,
    p => p.CategoryId,
    (Category, Products) =>
       new
       {
           CategoryType = Category,
           ItsProducts  = Products
       })
        .OrderBy(g => g.Category.Name)
        .Skip((CurrentPage - 1) * pageSize)
        .Take(pageSize);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM