0

这是我的课:

public class Schedule
{
    public string mintime { get; set; }
    public string maxtime { get; set; }
    public string weekday { get; set; }
}

我得到了这样的时间表列表,其中包含重复的工作日,例如

List<Schedule> list = new List<Schedule> {
    new Schedule { mintime = "01.01.2012", maxtime = "05.01.2012", weekday = "1" },
    new Schedule { mintime = "05.01.2012", maxtime = "10.01.2012", weekday = "1" },
    new Schedule { mintime = "10.01.2012", maxtime = "20.01.2012", weekday = "2" },
    new Schedule { mintime = "01.02.2012", maxtime = "10.02.2012", weekday = "2" },
    new Schedule { mintime = "01.03.2012", maxtime = "10.03.2012", weekday = "3" },
    new Schedule { mintime = "01.04.2012", maxtime = "10.04.2012", weekday = "4" }
};

如何获得 List> 的所有组合,例如:

List<List<Schedule>> list = new List<List<Schedule>> {
    new List<Schedule> { 
        new Schedule { mintime = "01.01.2012", maxtime = "05.01.2012", weekday = "1" },
        new Schedule { mintime = "10.01.2012", maxtime = "20.01.2012", weekday = "2" }, 
        new Schedule { mintime = "01.03.2012", maxtime = "10.03.2012", weekday = "3" },
        new Schedule { mintime = "01.04.2012", maxtime = "10.04.2012", weekday = "4" }
    },
    new List<Schedule> { 
        new Schedule { mintime = "01.01.2012", maxtime = "05.01.2012", weekday = "1" },
        new Schedule { mintime = "01.02.2012", maxtime = "10.02.2012", weekday = "2" },
        new Schedule { mintime = "01.03.2012", maxtime = "10.03.2012", weekday = "3" },
        new Schedule { mintime = "01.04.2012", maxtime = "10.04.2012", weekday = "4" }
    },
    new List<Schedule> { 
        new Schedule { mintime = "05.01.2012", maxtime = "10.01.2012", weekday = "1" },
        new Schedule { mintime = "10.01.2012", maxtime = "20.01.2012", weekday = "2" },
        new Schedule { mintime = "01.03.2012", maxtime = "10.03.2012", weekday = "3" },
        new Schedule { mintime = "01.04.2012", maxtime = "10.04.2012", weekday = "4" }
    },
    new List<Schedule> { 
        new Schedule { mintime = "05.01.2012", maxtime = "10.01.2012", weekday = "1" },
        new Schedule { mintime = "01.02.2012", maxtime = "10.02.2012", weekday = "2" },
        new Schedule { mintime = "01.03.2012", maxtime = "10.03.2012", weekday = "3" },
        new Schedule { mintime = "01.04.2012", maxtime = "10.04.2012", weekday = "4" }
    },
};

?

是否可以使用 LINQ 获得它?


我的问题的主要主题是建立时间表列表。每个时间表应包括周一、周二、周三、周四、周五、周六和周日一次。我需要使用一个更常见的列表来构建这样的列表,其中包含周一、周三等的许多时间表。

4

1 回答 1

2

在这里使用Eric Lippert's CartesianProduct

List<List<Schedule>> newList = CartesianProduct(list.GroupBy(l => l.weekday))
                               .Select(x => x.ToList())
                               .ToList();

--

public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(IEnumerable<IEnumerable<T>> sequences)
{
    IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
    return sequences.Aggregate(
        emptyProduct,
        (accumulator, sequence) =>
        from accseq in accumulator
        from item in sequence
        select accseq.Concat(new[] { item }));
}
于 2012-09-25T11:48:26.797 回答