开发者

What’s the Linq to SQL equivalent to CEILING?

How do 开发者_如何学JAVAI do this

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

in Linq to SQL?


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜