0

嗨,我正在尝试找出给定年份和月份的每个月的上半月和下半月如何将项目分组?

IE

比如说我必须在每个月的 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

我将如何拆分它,所以我想在一年/一个月内将航班按两周分组

IE

2012 年 7 月第 1 周和第 2 周的航班

2012 年 7 月第 3 周和第 4 周的航班

所以这就是我到目前为止所拥有的......最终它必须是某种使用自动映射器的视图模型等我还不确定它是如何将它巧妙地融入某种 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 }
                                                  } 

                                 }
                            };
4

1 回答 1

0

我特此粘贴 Linq 代码以满足要求。但不确定,这仍然可以以最短的方式实现。如果是这样,请告诉我。

 var monthGroupedDates = from d in dates
                            group d by d.Month into fd
                            select new { Month = fd.Key, Dates = fd };

        foreach (var g in monthGroupedDates)
        {
            var groupByHalf = from n in g.Dates
                              group n by (n.Day <= 15 ? 1 : 2) into gd
                              orderby gd.Key 
                              select new { WhichHalf = gd.Key, Dates = gd };
            foreach (var gh in groupByHalf)
            {
                string printString = (gh.WhichHalf == 1 ? "First" : "Second") + " week dates are:";
                foreach (var h in gh.Dates)
                {
                    printString += h.ToString("dd/MM/yyyy") + ";";
                }
                Console.WriteLine(printString);
            }

        }  

请注意,日期是您的要求中提到的日期。如果它的 Linq with SQL,这可能需要稍作改动。

拉吉

于 2012-08-23T07:55:27.090 回答