【发布时间】:2012-08-23 06:15:12
【问题描述】:
您好,我想知道您如何将给定年份和月份的每个月的上半月和下半月的项目分组?
即
例如,我必须在每个月的 6 号和每个月的 15 号列出项目的名称。
比如说我有
Flight Name Flight Date
Flight 1 01/07/2012
Flight 2 12/07/2012
Flight 3 18/07/2012
Flight 4 28/07/2012
我将如何拆分它,所以我想在一年/一个月内按两周对航班进行分组
即
2012 年 7 月第 1 周和第 2 周的航班
2012 年 7 月第 3 周和第 4 周的航班
这就是我目前所拥有的.. 最终它必须是某种使用 automapper 的视图模型等我还不确定它是如何将它巧妙地融入某种 ViewModel 形式...
var flightEntities = from f in flightsAsViewModels
select new
{
YearGroups = from flightYearGroup in context.Flights
group flightYearGroup by flightYearGroup.FlightDateTime.Year
into yearGroup
orderby yearGroup.Key descending
select new
{
Year = yearGroup.Key,
MonthGroups = from flightMonthGroup in yearGroup
group flightMonthGroup by flightMonthGroup.FlightDateTime.Month
into monthGroup
orderby monthGroup.Key ascending
select new {
Month = monthGroup.Key,
HalfMonthGroups = from months in monthGroup
group months by (months.FlightDateTime.Day <= 15 ? 1 : 2) into splitMonthFlights
orderby splitMonthFlights.Key
select new { WhichHalfOfMonth = splitMonthFlights.Key, Flights = splitMonthFlights }
}
}
};
【问题讨论】:
-
第 5 周怎么样?几乎每个月都有超过 4 周的天数。
-
这是在 LINQ to SQL (etc) 还是 LINQ to Objects 中? (LINQ to Objects 会更容易...)
-
@JonSkeet:根据标签,它是 LINQ to SQL
-
不清楚你想要什么。双月刊是每两个月一次。两周是14天。但我认为你想要每个月的 1-15 和 16-lastday 的时期。你能确认一下吗?
-
嗨 Gert 是的,这是正确的,谢谢,基本上从 1-15 和 16 到月底最后一天的时间段是完美的,谢谢.. 是的 linq to sql 并最终在视图模型中表示
标签: asp.net linq linq-to-sql