LINQ version of TOP PERCENT

2020-02-14 03:41发布

Is there a way to perform a top (Take) linq query using percentage? The T-SQL would have been:

SELECT TOP 20 PERCENT ...

But LINQ seems to only want an int.

It seems that I would have to do a count and then a take. Any suggestions?

2条回答
成全新的幸福
2楼-- · 2020-02-14 04:00

I don't think there's anything built into LINQ to SQL. You could do it with a stored procedure. I don't like that much, unless you happen to be using a procedure anyway, but it's probably better than doing it with two separate queries.

查看更多
女痞
3楼-- · 2020-02-14 04:25

You would have to perform the query twice, in essence. You would have to perform it once to get a count, and then again to figure out the percentage (because you will pass the number that corresponds to the count that will equal 20%).

查看更多
登录 后发表回答