Search code examples
sql-server-2005linq-to-sqlkeywordequivalentceil

What’s the Linq to SQL equivalent to CEILING?


How do I do this

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

in Linq to SQL?


Solution

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