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.