LINQ IQueryable 使用 skip 和 take 返回相同的行
LINQ IQueryable returning same rows with skip and take
使用 MVC Entity Framework 我正在使用 AJAX 调用一个函数,该函数传入 skip 并获取参数。
[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 多行要从中获取。
//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();
解决方案是按照 Damien_The_Unbeliever
的建议添加另一个 order by 子句
使用 MVC Entity Framework 我正在使用 AJAX 调用一个函数,该函数传入 skip 并获取参数。
[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 多行要从中获取。
//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();
解决方案是按照 Damien_The_Unbeliever
的建议添加另一个 order by 子句