© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
77 replies
TheHelpfulHelper

❔ LINQ: Group and extract all consecutive elements from List that match predicate

Example:
List<int> ints = new() {6,3,1,1,1,1,1,2,7,8,1,1,1,1,3,2,5,1,1,9,8,1,1,1,1,4,7,8,1}
var output = ints.GroupConsecutive().Where((g) => g.Key == 1); // Not a real function

foreach (var o in output) 
{
  Console.WriteLine(o.Count);
}
List<int> ints = new() {6,3,1,1,1,1,1,2,7,8,1,1,1,1,3,2,5,1,1,9,8,1,1,1,1,4,7,8,1}
var output = ints.GroupConsecutive().Where((g) => g.Key == 1); // Not a real function

foreach (var o in output) 
{
  Console.WriteLine(o.Count);
}

Expected output:
5
4
2
4
1
5
4
2
4
1


Is there such functionality in LINQ?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

❔ Predicate in LINQ GroupBy
C#CC# / help
4y ago
List not returning all matching elements
C#CC# / help
2y ago