Well, there’s a terrible way to do this:
int tCounter = 0;
var groups = sequence.GroupBy(x => x.Contains("t") ? ++tCounter : tCounter)
.Select(group => group.ToList())
.ToList();
or equivalent (but without calling Select):
int tCounter = 0;
var groups = sequence.GroupBy(x => x.Contains("t") ? ++tCounter : tCounter,
(count, group) => group.ToList())
.ToList();
It depends on the side effect in the article GroupBy- this is a really bad idea. LINQ is designed around functional ideals where queries should not have side effects. You put side effects in the code that uses the request, and not in the request itself. This will work, but I would not recommend it.
, , , :
using System;
using System.Collections.Generic;
using System.Linq;
public class Test
{
static void Main(string[] args)
{
var input = new List<string>{"a","b","c","at","h","c","bt"};
int tCounter = 0;
var groups = input.GroupBy(x => x.Contains("t") ? ++tCounter : tCounter)
.Select(group => group.ToList())
.ToList();
foreach (var list in groups)
{
Console.WriteLine(string.Join(", ", list));
}
}
}
:
a, b, c
at, h, c
bt
"" (, , , ), , . Ts, , GroupBy .
, IIRC - Reactive Extensions System.Interactive assembly . . LINQ.