找出项目所在的页面

时间:2013-11-12 14:27:59

标签: c# linq entity-framework

我在我的应用程序中使用LINQ和实体框架。我有一个存储库方法来获取这样的数据页面:

public IEnumerable<Sample> GetPageData(int orderId, int page, int itemsPerPage)
{
    var samples = _context.Set<Sample>()
                          .Where(s => s.OrderId == orderId)
                          .OrderBy(s => s.Id)
                          .Skip(itemsPerPage * page)
                          .Take(itemsPerPage);

    return samples;
}

我想有另一个存储库方法,以便我可以检索样本所在的页面。方法签名类似于:

public int GetPage(int orderId, int sampleId, int itemsPerPage)
{
    // ???
}

我正在努力寻找在LINQ中做到这一点的方法。我现在唯一的想法就是一次又一次地获取页面,直到我找到所需的样本。我知道它效率不高,但要求是样本不超过500个,页面大小为25。

我如何才能更有效地做到这一点?

4 个答案:

答案 0 :(得分:3)

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);
}

答案 1 :(得分:2)

public int GetPage(int sampleId, int itemsPerPage)
{
    return _context.Set<Sample>()
                   .Count(s => s.Id <= sampleId) / itemsPerPage;
}

答案 2 :(得分:1)

public int GetPage(int orderId, int sampleId, int itemsPerPage)
{
    var samples = _context.Set<Sample>()
        .Where(s => s.OrderId == orderId)
        .OrderBy(s => s.Id)
        .Where(s => s.Id <= sampleId)
        .ToList();

    var lastSample = samples.LastOrDefault();
    if (lastSample == null || lastSample.Id != sampleId) return -1;

    return (samples.Count - 1) / itemsPerPage;
}

答案 3 :(得分:0)

这里的假设是(至少)给定sampleId和orderId的条目。

public int GetPage(int sampleId, int itemsPerPage, int orderId)
{
   return _context.Set<Sample>().Count(s => s.OrderId == orderId && s.Id < sampleId) / itemsPerPage + 1;                
}