使用skip和take返回相同的行
本文关键字:返回 skip take 使用 | 更新日期: 2023-09-27 17:54:49
使用MVC实体框架,我用AJAX调用一个函数,传入跳过和接受参数。
[HttpGet]
public async Task<ActionResult> _ViewMore( int take, int skip)
{
var c = await GetContent( take, skip);
return View(c)
}
public async Task<List<PartialContent>> GetContentForCulture(int take, int skip)
{
return await ContextHelper.SearchContent(take, skip);
}
public static async Task<List<PartialContent>> SearchContent( int take, int skip)
{
try
{
using (var context = new Context())
{
var content = context.ContentEntities.SearchContent(take, skip);
var f = await content.Select(s => new PartialContent
{
Subtype = s.Subtype,
Id = s.Id,
MainImage = s.MainImage,
}).ToListAsync();
return f;
}
}
catch (Exception ex)
{
// Log.Err(ex.Message, ex);
return null;
}
}
public static IQueryable<T> SearchContent<T>(this IQueryable<T> source, int take, int skip)
where T : ContentEntity
{
source.Where(m => m.IsPublished ).OrderByDescending(m => m.DatePublished).Skip(skip).Take(take)
}
我的问题是,每次我调用函数返回相同的行,即使我调试和跳过值增量,我有100行获取。
解决方案是按照Damien_The_Unbeliever的建议添加另一个order by子句
//pageSize is how many rows you want to skip every time and
//index is like page number first time index should be 0 then 1 every time index will be increased by one
var c = await context.ContentEntities.Skip(pageSize * index).Take(pageSize).ToListAsync();