这应该有效:
var data = list.OrderBy(x => x.Id);
bool approved = data.First().Approved;
var groups = new List<Tuple<bool, List<Foo>>>();
var all = groups.SelectMany(x => x.Item2);
while (all.Count() != list.Count())
{
groups.Add(
Tuple.Create(
approved
, list.Skip(all.Count())
.TakeWhile(x => x.Approved == approved)
.ToList()));
approved = !approved;
}
foreach (var grp in groups)
{
Console.Write("Next block: {0} with {1} elements", grp.Item1, grp.Item2.Count);
}
这是你的课:
class Foo
{
public int Id { get; set; }
public bool Active { get; set; }
public DateTime datetime { get; set; }
public bool Approved { get; set; }
}
和样本数据:
var list = new List<Foo>();
list.Add(new Foo() { Id = 1, Active = true, Approved = true });
list.Add(new Foo() { Id = 2, Active = true, Approved = true });
list.Add(new Foo() { Id = 3, Active = true, Approved = true });
list.Add(new Foo() { Id = 4, Active = true, Approved = false });
list.Add(new Foo() { Id = 5, Active = true, Approved = false });
list.Add(new Foo() { Id = 6, Active = true, Approved = false });
list.Add(new Foo() { Id = 7, Active = true, Approved = true });
list.Add(new Foo() { Id = 8, Active = true, Approved = true });
list.Add(new Foo() { Id = 9, Active = true, Approved = false });