linq group by contiguous blocks

时间:2013-02-07 10:11:55

标签: c# linq

假设我有以下数据:

  

时间状态
  10:00在   11:00关闭   12:00关闭   13:00关闭   14:00关闭   15:00在   16:00开始

我如何使用Linq将其分组为

  

[On,[10:00]],[Off,[11:00,12:00,13:00,14:00]],[On,[15:00,16:00]]

5 个答案:

答案 0 :(得分:11)

创建GroupAdjacent扩展名,例如列出的here

然后它就像:

一样简单
var groups = myData.GroupAdjacent(data => data.OnOffStatus);

答案 1 :(得分:5)

您也可以使用一个Linq查询使用变量来跟踪更改,例如:

int key = 0;
var query = data.Select(
    (n,i) => i == 0 ? 
        new { Value = n, Key = key } : 
        new 
        { 
            Value = n, 
            Key = n.OnOffFlag == data[i - 1].OnOffFlag ? key : ++key 
        })
    .GroupBy(a => a.Key, a => a.Value);

基本上,它为每个项目分配一个键,当当前项目不等于前一项目时,该项目会增加。当然,这假设您的数据在List或Array中,否则您必须尝试不同的方法

答案 2 :(得分:3)

这是一个硬核 LINQ解决方案,它使用Enumerable.Zip来比较连续元素并生成一个连续的密钥:

var adj = 0;
var t = data.Zip(data.Skip(1).Concat(new TimeStatus[] { null }),
        (x, y) => new { x, key = (x == null || y == null || x.Status == y.Status) ? adj : adj++ }
    ).GroupBy(i => i.key, (k, g) => g.Select(e => e.x));

答案 3 :(得分:1)

可以这样做。

  1. 迭代收集。
  2. 使用TakeWhile<Predicate>条件是集合的第一个元素的文本On或Off。
  3. 迭代第一点的子集并重复上面的步骤并连接字符串。
  4. 希望有所帮助......

答案 4 :(得分:1)

您可以解析列表并分配一个连续的键,例如定义一个类:

public class TimeStatus
{
    public int ContiguousKey { get; set; }
    public string Time { get; set; }
    public string Status { get; set; }
}

您可以通过循环,维护计数以及检测状态何时从开启​​变为关闭等等来为连续键指定值,这样就可以得到如下列表:

List<TimeStatus> timeStatuses = new List<TimeStatus> 
            {
                new TimeStatus { ContiguousKey = 1, Status = "On", Time = "10:00"},
                new TimeStatus { ContiguousKey = 1, Status = "On", Time = "11:00"},
                new TimeStatus { ContiguousKey = 2, Status = "Off", Time = "12:00"},
                new TimeStatus { ContiguousKey = 2, Status = "Off", Time = "13:00"},
                new TimeStatus { ContiguousKey = 2, Status = "Off", Time = "14:00"},
                new TimeStatus { ContiguousKey = 3, Status = "On", Time = "15:00"},
                new TimeStatus { ContiguousKey = 3, Status = "On", Time = "16:00"}
            };

然后使用以下查询,您可以提取状态和分组时间:

    var query = timeStatuses.GroupBy(t => t.ContiguousKey)
    .Select(g => new { Status = g.First().Status, Times = g });
相关问题