相关疑难解决方法(0)

获取给定日期的正确周数

我搜索了很多并找到了很多解决方案,但没有一个能给我2012-12-31的正确周数.甚至MSDN(链接)上的示例都失败了.

2012-12-31是星期一,因此它应该是第1周,但我尝试的每种方法都给了我53.以下是我尝试过的一些方法:

来自MDSN图书馆:

DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
Calendar cal = dfi.Calendar;

return cal.GetWeekOfYear(date, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);
Run Code Online (Sandbox Code Playgroud)

解决方案2:

return new GregorianCalendar(GregorianCalendarTypes.Localized).GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
Run Code Online (Sandbox Code Playgroud)

解决方案3:

CultureInfo ciCurr = CultureInfo.CurrentCulture;
int weekNum = ciCurr.Calendar.GetWeekOfYear(dtPassed, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
return weekNum;
Run Code Online (Sandbox Code Playgroud)

更新

当日期为2012-12-31时,以下方法实际返回1.换句话说,我的问题是我的方法不符合ISO-8601标准.

// This presumes that weeks start with Monday.
// Week 1 is the 1st week of the year with a Thursday in it.
public static int GetIso8601WeekOfYear(DateTime time)
{
    // Seriously cheat.  If its Monday, Tuesday or Wednesday, then it'll 
    // be …
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net date

198
推荐指数
6
解决办法
21万
查看次数

C#List <> GroupBy 2值

我在Framework 3.5上使用C#.我希望通过两个属性快速对Generic List <>进行分组.为了这个例子,我假设我有一个Order类型的List,其中包含CustomerId,ProductId和ProductCount属性.如何使用lambda表达式获取CustomerId和ProductId分组的ProductCounts总和?

c# linq ienumerable group-by list

34
推荐指数
3
解决办法
7万
查看次数

如何按周分组日期?

我正在为我正在创建的定制应用程序编写Excel导出器,我对C#中的LINQ分组有疑问.

基本上,这个新的Excel导出器类有两个日期.然后,该班级检索此日期范围之间的所有托运.

作为此导出器的一部分,我需要能够将日期分组为几周,并获取该周的值.因此,例如,如果我给了07/12/2011和22/12/2011(dd/MM/yyyy格式),我需要将它们之间的所有货物分组为几周(每周从星期日开始).使用上述日期的理想结果是

Week 1: (consignments between 04/12/2011 and 10/12/2011) 
Week 2: (consignments between 11/12/2011 and 17/12/2011) 
Week 3: (consignments between 18/11/2011 and 24/12/2011)
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

c# linq grouping

20
推荐指数
3
解决办法
1万
查看次数

标签 统计

c# ×3

linq ×2

.net ×1

asp.net ×1

date ×1

group-by ×1

grouping ×1

ienumerable ×1

list ×1