public int GetPage(int orderId, int sampleId, int itemsPerPage)
{
//protect against divide by zero
if(itemsPerPage < 1)
return 1;//or 0 if you want page index
int index = _context.Set<Sample>()
.Where(s => s.OrderId == orderId && s.Id < sampleId)
//.OrderBy(s => s.Id) edited after accepted OrderBy not necessary
.Count();
//if index is zero return 1
//if index == 9 and itemsPerPage == 10 return 1
//if index == 10 and itemsPerPage == 10 return 2
//if you want the page index rather than the page number don't add 1
return 1 + (index / itemsPerPage);
}
@Rob Lyndon 的努力让我想了更多,我想出了这个方法来检查页面是否实际包含样本 - 在对数据库的一次查询中
public int GetPage(int orderId, int sampleId, int itemsPerPage)
{
//protect against divide by zero
if(itemsPerPage < 1)
return 1;//or 0 if you want page index, or -1 if you want to flag this as invalid
var result = context.Set<Sample>()
.Where(s => s.OrderId == orderId
&& s.Id <= sampleId)//this time include sampleId
//.OrderBy(s => s.ID) edited after accepted OrderBy not necessary
.GroupBy(x => true)
.Select(group => new
{
MaxID = group.Max(s => s.Id),
Count = group.Count()
})
.Single();
//Check the sample is actually in the result
if(result.MaxID != sampleId)
return 1;//or 0 if you want page index, or -1 if you want to flag this as invalid
int index = result.Count - 1;
//if you want the page index rather than the page number don't add 1
return 1 + (index / itemsPerPage);
}