I need help with a linq query that will return true if the list contains x objects in a row when the list is ordered by date.
so like this:
myList.InARow(x => x.Correct, 3)
would return true if there are 3 in a row with the property correct == true.
Not sure how to do this.
Using a GroupAdjacent
extension, you can do:
var hasThreeConsecutiveCorrect
= myList.GroupAdjacent(item => item.Correct)
.Any(group => group.Key && group.Count() >= 3);
Here's another way with a Rollup
extension (a cross between Select and Aggregate) that's somewhat more space-efficient:
var hasThreeConsecutiveCorrect
= myList.Rollup(0, (item, sum) => item.Correct ? (sum + 1) : 0)
.Contains(3);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With