2

How do I do this

SELECT  CEILING(COUNT(*) / 10) NumberOfPages
 FROM  MyTable

in Linq to SQL?

ACP
  • 34,682
  • 100
  • 231
  • 371

3 Answers3

1

Many .NET methods are translated to SQL Server functions, such as most of the methods of the Math class and the String class. But there are some caveats.

Also have a look at the SqlMethods class, which exposes additional SQL Server function that doesn't have a .NET equivalent.

But you don't even need any of that in your case:

int numberOfPages;

using (var db = new MyDBDataContext())
{
   numberOfPages = (int)Math.Ceiling(db.Books.Count() / 10.0);
}
Allon Guralnek
  • 15,813
  • 6
  • 60
  • 93
0

You dont use SQL CEILING, you use .NET ceiling (Math.Ceiling) in your LINQ query.

Francisco Soto
  • 10,277
  • 2
  • 37
  • 46
0

I don't think this is possible. A possible solution would be to get the total count and then figure it out in .NET code. Like below:

where query is IQueryable

  var itemsPerPage = 10; 
  var currentPage = 0; 
  var totalResults = query.Count(); 
  var myPagedResults = query.Skip(currentPage).Take(itemsPerPage);
  var totalPages = (int)Math.Ceiling((double)totalResults / (double)pageSize);
willbt
  • 1,875
  • 13
  • 11