0

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.

Tim M.
  • 53,671
  • 14
  • 120
  • 163
  • You provide no context to this question, My first instinct tells me whatever condition youre trying to check in isTrue should be checked before the Take(), you could get Count() items of your list, but that would be really inefficient and wrong – downrep_nation Jun 08 '18 at 15:02

2 Answers2

5

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

...
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • Why to take all the items and create overhead on system when we only required 10 items. – habib Jun 08 '18 at 15:35
  • @habib You aren't unless you put a `ToList` in the wrong spot (lazy enumeration). This is just explicitly writing the same line that `.` chaining does – BradleyDotNET Jun 08 '18 at 15:36
1

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);
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • 1
    Putting a link to how bad "suboptimal" can be would be useful :-) But they are difficult to find (the good ones)... This link is suboptimal too :-) https://stackoverflow.com/q/28756/613130 – xanatos Jun 08 '18 at 15:48
  • @xanatos depends on the use case I guess but "sub-optimal" sometimes doesn't matter at all ;-). I'd say if you like code golfing like me then I'd proceed with the current approach unless I know it will cause me performance issues but then again others would argue if it's production code then I'd use the `if` statement for readability et al... – Ousmane D. Jun 08 '18 at 16:02
  • Just to be super clear. "Suboptimal" means that it enumerates the **entire** list to get the count (so best case its now enumerated twice) – BradleyDotNET Jun 08 '18 at 16:10
  • @BradleyDotNET as mentioned that depends what type `query` _actually_ is. depending on the actual type, one can avoid "enumerating twice". – Ousmane D. Jun 08 '18 at 16:12
  • Sure; I thought that was clear but this only applies to the `IEnumerable` case :) – BradleyDotNET Jun 08 '18 at 16:13
  • @BradleyDotNET Right, but still saying "so best case its now enumerated twice" is false. best case is actually _once_. – Ousmane D. Jun 08 '18 at 16:22
  • Ok, best case *if you are doing this with an `IEnumerable`* is enumerating twice. Better? – BradleyDotNET Jun 08 '18 at 16:28
  • @BradleyDotNET close but not quite, even _if you are doing this with an IEnumerable_ it will only enumerate _twice_ if `isTrue` is false. now that's much better ;) – Ousmane D. Jun 08 '18 at 16:29
  • Ok, fair enough :) – BradleyDotNET Jun 08 '18 at 16:30