I have the following code
return lStoredRecords.Select(u =>
u.sCardId == sCardId &&
u.rtMode == eRecordType &&
u.dtTime >= DateTime.Now.AddMinutes(-15.0)).Any();
But for some reason it always returns True,
lStoredRecords contains one record such that it’s dtTime doesn’t satisfy the above condition – so I was expecting the above line of code to return False
Any ideas?
Firstly, it’s not clear why you’re using
Selectat all. I’d expect you to just useAnyon its own, specifying the predicate as an argument.Secondly, if
lStoredRecordshas any records at all, this will always returntrue–Any()without a predicate just returns whether there are any records in the input sequence, andSelectis just going to project an input value to an ouptut value – it’s not doing any filtering at all. If you were expecting to filter, you should have been usingWhere.Thirdly, it’s not clear that you want
Anyin the first place. From your question:If you want it to return
falseif any of the records fails to satisfy a condition, you should almost certainly be usingAllinstead – you want to check whether all the input values match your condition.So basically you want: