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

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

我使用 MVC Entity Framework 调用一个带有 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 子句

关于c# - LINQ IQueryable 使用 skip 和 take 返回相同的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38188869/

相关文章:

c# - 如何构造C#类库?

c# - 如何防止访问 ASP.NET 中的文件夹

c# - 在 DataGrid WPF 中查找文本框

asp.net-mvc - JQGrid 和 MVC 完整工作示例

c# - 无法保存到数据库或从中检索(正确的信息)

c# - 系统聚合异常 : Azure can't deploy website

c# - Razor 中的函数和助手有什么区别?

c# - 为什么我在使用带有安全 URL 的 HttpResponseMessage 时得到 401 未经授权?

asp.net - 如何使用另一个表中的列来引用两个表 - Entity Framework

c# - 如何使 M :N (many-to-many) relationship where both M and N are the same entities?