LINQ IQueryable使用skip和take返回相同的行

时间:2016-07-04 16:15:26

标签: c# asp.net-mvc entity-framework linq take

使用MVC实体框架我用AJAX调用一个传递skip和take参数的函数。

[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行需要从中获取。

2 个答案:

答案 0 :(得分:2)

解决方案是按照Damien_The_Unbeliever

的建议添加另一个order by子句

答案 1 :(得分:-2)

    //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();