0

I've created 2 pieces of code, the first one i will call query, the second one I will call code. I made this to ask my colleagues what they though was more readable.

var activeNotifications = from activeNot in mActiveNotifications
                            where (from filter in activeNot.Notification.StationFilters
                                    where (from stat in filter.Stations
                                        where stat.Equals(station)
                                        select stat).Any()
                                    select filter).Any()
                            select activeNot;

The code:

var activeNotifications = mActiveNotifications.Where(x => x.Notification.StationFilters.Where(filter => filter.Stations.Where(stat => stat.Identification.Equals(station)).Any()).Any());

But now I've got a problem because where('lambda').Any() is so much slower compared to: any('lambda'). So the faster code would look like this:

var activeNotifications = mActiveNotifications.Where(x => x.Notification.StationFilters.Any(filter => filter.Stations.Any(stat => stat.Identification.Equals(station))));

So I was wondering if there was a way to write the above code as a query since I'm not able to make a statement like: 'where any'. Does this mean the linq query will always be more slow compared to the code so we would have to trade performance against readability (if, it is more readable in the first place but that's a different discussion).

So is there a way to use 'any' in a linq query? or will i be better of using just code

P.S. I've been told .Any() is faster than Where/Select/Find/FirstOrDefault/etc. I have not tested if this is true but it seems more than likely to me since it can stop when it found something and only has to return a Boolean instead of the found object.

Vincent
  • 1,497
  • 1
  • 21
  • 44
  • FYI: those are known as Linq query syntax and Linq method syntax. – juharr Oct 17 '16 at 13:08
  • _" I've been told .Any() is faster than Where/Select/Find/FirstOrDefault/etc. I have not tested if this is true but it seems more than likely to me since it can stop when it found something"_ `First` and `FirstorDefault` will also return immediately. `Where` uses deferred execution so is basically a no-op. If you use `Where(...).First()` it will also break and return immediately. – Tim Schmelter Oct 17 '16 at 13:10
  • What makes you think that `Where(lambda).Any()` is much slower than `Any(lambda)`? – juharr Oct 17 '16 at 13:11
  • http://stackoverflow.com/questions/25787555/linq-performance-count-vs-where-and-count – René Vogt Oct 17 '16 at 13:29
  • @juharr thanks for the syntax, makes it easier to differentiate when talking about them. I think where first executes the where going through all the lists and create it's own list of objects it found and afterwards use any to check if there is something in said list. Which, in my eyes would make it quite a bit slower. – Vincent Oct 17 '16 at 13:29
  • 1
    @VincentAdvocaat that's not true, `Where` uses _deferred_ execution, so the lists are iterated only until the `Any` found the first matching element. – René Vogt Oct 17 '16 at 13:32
  • @RenéVogt see, that's why we say: assumption is the mother of all f-ups. I shouldn't just think i know stuff without checking them. So the linq query and linq method should be just as fast either way and in essence are the same? – Vincent Oct 17 '16 at 13:37
  • @VincentAdvocaat the question I linked above shows some cases where the two versions may differ and `Where(lambda).Any()` could be faster thatn `Any(lambda)`, but I did not verify the answers myself, plus they are talking about `Count()` and I'm not sure if they apply to `Any()` as well. – René Vogt Oct 17 '16 at 13:40

0 Answers0