Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用DbContext的服务器端分页和OrderBy_C#_Asp.net Mvc 3_Entity Framework 4.1 - Fatal编程技术网

C# 使用DbContext的服务器端分页和OrderBy

C# 使用DbContext的服务器端分页和OrderBy,c#,asp.net-mvc-3,entity-framework-4.1,C#,Asp.net Mvc 3,Entity Framework 4.1,有没有人能告诉我们如何使用DbContext来执行服务器端分页和orderby功能。我使用Where子句来过滤记录。我需要类似的东西,这样我就只能得到有限数量的记录 提前感谢您可以在EF中使用and builder方法执行此操作,或者在调用OrderBy后使用LINQSkip和Take检查此解决方案。也许这就是你想要的: public const int PAGE_SIZE = 10; protected void LinqDataSource1_Selecting(object sender,

有没有人能告诉我们如何使用DbContext来执行服务器端分页和orderby功能。我使用Where子句来过滤记录。我需要类似的东西,这样我就只能得到有限数量的记录


提前感谢

您可以在EF中使用and builder方法执行此操作,或者在调用
OrderBy
后使用LINQ
Skip
Take
检查此解决方案。也许这就是你想要的:

public const int PAGE_SIZE = 10;
protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
// LINQ query
    var query = from m in db.Products
    select m;
    // Set the total count
    // so GridView knows how many pages to create
    e.Arguments.TotalRowCount = query.Count();
    // Get only the rows we need for the page requested
    query = query.Skip(GridView1.PageIndex * PAGE_SIZE).Take(PAGE_SIZE);
    e.Result = query;
}

您好,我的存储库中没有MyDbContext。怎么做?这就是我试图做的
code
IQueryable加载数据(表达式filter=null,Func orderBy=null,int currentPage=0,int pageSize=0,string includeProperties=“”){IQueryable查询=_context.Set()。其中(filter).AsNoTracking();IOrderedQueryable orderedQuery;IQueryable returnValue=null;if(orderBy!=null)query=orderBy(query);if(pageSize>0)returnValue=query.Skip((currentPage-1)*pageSize).Take(pageSize).AsQueryable();return returnValue;}
code
虽然这从理论上可以回答问题,但要在这里包括答案的基本部分,并提供链接供参考。