How can I add a condition to my Take()
call?
query.Take(isTrue ? 10 : 0);
Instead of 0, I would like to take all the items returned by the query.
How can I add a condition to my Take()
call?
query.Take(isTrue ? 10 : 0);
Instead of 0, I would like to take all the items returned by the query.
There isn't really an "All" parameter to Take
but you can conditionally apply the clause.
IEnumerable query = something;
if (isTrue)
query = query.Take(10);
...
if you really want to use the conditional operator then this would be the way to proceed:
var result = query.Take(isTrue ? 10 : query.Count());
Although, I have to admit if query
is an IEnumerable<T>
then this is suboptimal compared to the other answer.
if by any chance it's a list then an equivalent version in terms of performance would be:
var result = query.Take(isTrue ? 10 : query.Count);