Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ version of TOP PERCENT

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?

like image 377
Tony Basallo Avatar asked Jan 22 '09 18:01

Tony Basallo


People also ask

How is Linq percentage calculated?

var sum = (from temp in list select temp). Sum(); var percentage = from temp in list select temp/sum *100; the above one is optimized linq query. var percentage = from temp in list select (temp / list.

What is query expressions in C#?

A query expression is a first-class language construct. It is just like any other expression and can be used in any context in which a C# expression is valid. A query expression consists of a set of clauses written in a declarative syntax similar to SQL or XQuery.


1 Answers

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%).

like image 73
casperOne Avatar answered Nov 15 '22 12:11

casperOne