Get all unique variations

Here is class I have:

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

I got a list of such schedule with repeat week days like

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" }
};

How can I get List> with all combinations like:

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" }
    },
};

?

Is it possible to get it using LINQ?


The main topic of my question is to build list of schedules. Each schedule should include monday, tuesday, wednesday, thursday, friday, saturday and sunday once. And I need to build such list using a more common list that contains a many schedules for mondays, wednesdays and so on.


Using Eric Lippert 's CartesianProduct here

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/54888.html

上一篇: “重映射”一个简单的二进制掩码的最佳方法是什么?

下一篇: 获取所有独特的变化