获取所有独特的变化

这是我拥有的课程:

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<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获得它?


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


在这里使用Eric Lippert的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 }));
}
链接地址: http://www.djcxy.com/p/54887.html

上一篇: Get all unique variations

下一篇: Quartz.NET, Recur Every x Weeks