-1

I want to invoke Queryable.Where() and get all elements. There's no version of Where() that works without a predicate function. So I have to right this:

 var result = table.Where( x =>  true );

and it works but that feels really stupid to me - x is never used, and there's no "transformation" for the => "arrow" symbol.

Is there a more elegant solution?

3Dave
  • 28,657
  • 18
  • 88
  • 151
sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • 15
    Why do you need to call `Where` to get all results? Just omit the call. – Mark Byers Jun 06 '12 at 13:56
  • 1
    Why not leave it without predicate? `var result = table;` – archil Jun 06 '12 at 13:57
  • 2
    Agree with @Mark that this the call is not required, but even if it *were* required (because you were using some other method that consumed a lambda where eliding the call wouldn't make sense) there's nothing wrong with `x => true` in that situation. – Kirk Woll Jun 06 '12 at 13:58
  • 1
    "There's no version of `Where()` that works without a predicate function" - what situation do you have that requires one? – AakashM Jun 06 '12 at 13:58
  • 2
    if you tell us what you are you trying to achieve and in what context maybe we can help you – Massimiliano Peluso Jun 06 '12 at 14:01
  • @eldarerathis Fair enough. Waiting on coffee to kick in. upvote removed pending more details. – 3Dave Jun 06 '12 at 14:09

5 Answers5

11

You can use the following, which is more elegant:

var result = table;

You could also omit result completely, and use table directly.

devdigital
  • 34,151
  • 9
  • 98
  • 120
3

Isn't table.Where(x=>true) essentially a noop? I mean, what is the point? You can do use _ instead of x though, which is idiomatic.

table.Where(_=> true);

But really, the following is what you are doing:

for (var item in table)
{
    if (true) // your Where() clause..
    {
        yield item;
    }
}

See how it doesn't really make sense?

Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164
  • 2
    I didn't DV, but your answer essentially restates the OP's question -- the complaint that `x => true` seems like a noop. And I don't have the foggiest idea where you're going with the allegedly idomatic `_ => true`. Changing the name of the identifier from `x` to `_` is meaningless. – Kirk Woll Jun 06 '12 at 14:11
  • downvote didn't make sense to me but your explanation through code should definitely help newbies with Linq so +1 – Maverik Jun 06 '12 at 14:11
  • 1
    @KirkWoll, perhaps noop was a bad phrase to use - I meant it was pointless. And yes, the lambda variable `_` is used to signify that you aren't actually using the variable in the lambda body. – Josh Smeaton Jun 06 '12 at 14:13
  • 1
    @Josh, I see what you're saying on the noop now. And I also see now that [it is in fact idiomatic](http://stackoverflow.com/questions/424775/is-there-a-better-way-to-express-a-parameterless-lambda-than) in the way you describe. So +1. – Kirk Woll Jun 06 '12 at 14:14
3

table.Where( x => true ) is not "returning all elements". It simply returns an enumerable that has enough information to return some subset of elements when it is being enumerated upon. Until you enumerate it, no elements are "returned".

And since this subset is not even proper in this case (i.e. all elements are returned), this is essentially a no-op.

To enumerate over all elements, write a simple foreach, or use ToList or ToArray or if you don't care about actually returning any elements (and just want to enumerate, presumably for side-effects): table.All(x => true) or table.Any(x => false), or even just table.Count().

Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167
1

In this case you would not need to call Where because you are not filtering the Queryable.

If you still wish to call Where and you do this in many places you could define a static Func and reuse that:

    public static Func<int, bool> t = ReturnTrue;

    public static bool ReturnTrue(int i) 
    {
        return true;
    }

    table.Where(t);
xcopy
  • 2,248
  • 18
  • 24
0

If you're trying to get a copy of the contents of table instead of a reference,

var result = table.ToList();

but it's not clear if that's really what you're trying to accomplish. Details?

3Dave
  • 28,657
  • 18
  • 88
  • 151