我收集的数据是这样的

| title | network | start |  end  | type |
------------------------------------------
| FOO   |    1    | 02:00 | 05:00 |   1  |
| FOO   |    1    | 02:00 | 03:10 |   2  |
| FOO   |    1    | 03:11 | 04:00 |   2  |
| FOO   |    1    | 04:01 | 05:00 |   3  |
| BAR   |    1    | 05:01 | 07:00 |   3  |

下面是类型的解释
类型1是acontainer
类型2是apartial
类型3是asingle
而且您必须知道container包含所有的部分,如果标题相同,则可能包含single
IEnumerable<Item> GetPartials(Item container, IEnumerable<Item> collection)
{
    foreach(Item item in collection)
    {
        if(container.Network == item.Network
            && container.Title == item.Title)
            // there is obviously also a check on the times, but it's long to write
        {
            yield return item;
        }
    }
}

现在,我需要将这些部分聚合到容器中并移除它们。
考虑到我有一个像这样的方法
public Item Aggregate(Item container, IEnumerable<Item> partials)
{
    ... //this is irrelevant
    return container;
}

如果这样的话,我会在集合中循环
public IEnumerable<Item> FilterAndAggregate(IEnumerable<Item> collection)
{
    foreach(Item item in collection)
    {
        if(item.Type == 1)
        {
            yield return Aggregate(item, GetPartials(item, collection));
        }
        else if (item.Type == 3)
        {
            yield return item;
        }
        // I'm filtering the partials because I already aggregated them
    }
}

但正如你在上一个循环中看到的,我可能已经聚合了一个。
如何从Item中排除部分?

最佳答案

假设项目类是这样的

public class Item
{
    public string Title { get; set; }
    public int Network { get; set; }
    public string Start { get; set; }
    public string End { get; set; }
    public int Type { get; set; }

    public List<Item> Partials { get; set; }
}

这是密码。
    IEnumerable<Item> GetPartials(Item container, List<Item> collection)
    {
        for (int i = collection.Count - 1; i >= 0; i--)
        {
            if (container.Network == collection[i].Network
                && container.Title == collection[i].Title)
            // there is obviously also a check on the times, but it's long to write
            {
                yield return collection[i];
                collection.RemoveAt(i);//remove already checked item
            }
        }
    }

    public Item Aggregate(Item container, IEnumerable<Item> partials)
    {
        //this is irrelevant
        container.Partials = partials.ToList();
        return container;
    }

    public IEnumerable<Item> FilterAndAggregate(IEnumerable<Item> collection)
    {
        var col = collection as IList<Item> ?? collection.ToList();
        var partials = col.Where(x => x.Type == 2).ToList();
        var notPartials = col.Where(x => x.Type != 2).ToList();
        foreach (Item item in notPartials)
        {
            if (item.Type == 1)
            {
                yield return Aggregate(item, GetPartials(item, partials));
            }
            else if (item.Type == 3)
            {
                yield return item;
            }
            // I'm filtering the partials because I already aggregated them
        }
    }

如果有大量的记录会更好。

09-04 06:06