我希望我理解正確你。
給定一串日期,由今年星期分組:
// some testdata
var datesList = Enumerable.Range(0, 8).Select (e => DateTime.Now.AddDays(7 * e)).ToList();
Func<DateTime, int> weekProjector = d =>
CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(d, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
var weeks = datesList.GroupBy(weekProjector).ToList();
你應該得到你想要通過這個查詢結果:
var result = weeks.OrderBy(w => w.Key)
.Select((g, i) => new {WeekType = (i/rotation) % 2, Week = g})
.GroupBy(w => w.WeekType)
.ToList();
其中rotation
是你的「旋轉」 (2
或4
)和2
是要(組A
和B
=> 2基團)基團的數目。
您可能要添加到Select
的GroupBy
後,只選擇你想要的數據,例如:
var weekA = result.Where(r => r.Key==0).Select(g => g).SelectMany(g => g).SelectMany(a => a.Week).ToList();
var weekB = result.Where(r => r.Key==1).Select(g => g).SelectMany(g => g).SelectMany(a => a.Week).ToList();
更可讀/通用的解決方案:
void Rotate<TResult, TGroup>(IEnumerable<TResult> datesList, Func<TResult, TGroup> grouper, int rotation, out List<TResult> listA, out List<TResult> listB)
{
listA = new List<TResult>();
listB = new List<TResult>();
var weeks = datesList.GroupBy(grouper).ToList();
var c_rotation = 0;
var c_list = listA;
using (var en = weeks.GetEnumerator())
while(en.MoveNext())
{
c_list.AddRange((IGrouping<TGroup, TResult>)en.Current);
c_rotation++;
if (c_rotation == rotation)
{
c_rotation = 0;
c_list = c_list == listA ? listB : listA;
}
}
}
使用它喜歡:
List<DateTime> listA;
List<DateTime> listB;
Func<DateTime, int> weekProjector = d =>
CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(d, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
Rotate(datesList, weekProjector, 2, out listA, out listB);
我很難理解你的問題。你能否給出一個更完整的例子,說明你希望達到的目標,顯示星期的最初狀態和最終狀態? – 2013-02-18 12:54:56
什麼是A周和B周? – 2013-02-18 12:55:16
@Tim當週收集有4周的分組,周旋轉值爲2時,前2周爲A周,最後2周爲B周。我認爲這從我上面的示例中可以清楚看出。我希望現在清楚:) – Elisabeth 2013-02-18 12:58:07