What's the Linq to SQL equivalent for CEILING?

How to do it

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

      

in Linq to SQL?

+2


a source to share


3 answers


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

Also consider the SqlMethods class , which provides additional SQL Server functionality that has no .NET equivalent.



But you don't even need anything in your case:

int numberOfPages;

using (var db = new MyDBDataContext())
{
   numberOfPages = (int)Math.Ceiling(db.Books.Count() / 10.0);
}

      

+1


a source


You are not using SQL CEILING, you are using .NET ceiling (Math.Ceiling) in LINQ query.



0


a source


I do not think that's possible. A possible solution would be to get the total and then figure it out in .NET code. Like below:

where the 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);

      

0


a source







All Articles