当前位置: 首页 > 编程语言 > C#

LinqtoSql–DateTimeFormat–YYYY-MMM(2009年3月)分享

时间:2023-04-10 14:54:39 C#

LinqtoSql–DateTimeFormat–YYYY-MMM(2009年3月)按用户名和日期时间。我希望DateTime的格式类似于“YYYY-MMM”(即2009年3月)。我认为这可行,但LinqtoSql无法解析ToString“格式”参数。dc.MyTable.GroupBy(r=>new{Name=r.UserName,YearMonth=r.SomeDateTime.ToString("yyyy-MMM")}).Select(r=>new{Name=r.UserName,YearMonth=r.YearMonth,Count=r.Count()}).OrderBy(r=>r.YearMonth).ThenBy(r=>r.Name);有人有什么想法/建议吗?谢谢。我想知道您是否应该以“长”方式执行此操作...dc.MyTable.GroupBy(r=>new{Name=r.UserName,Year=r.SomeDateTime.Year,Month=r.SomeDateTime.Month}).Select(r=>new{Name=r.UserName,Year=r.Year,Month=r.Month,Count=r.Count()}).OrderBy(r=>r.Year).ThenBy(r=>r.Month).ThenBy(r=>r.Name);如果您需要字符串形式的格式,请稍后在UI中执行。通过重建DateTime等,或使用CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(...)。我会避免将任何东西变成数据库中的字符串。通过r.SomeDateTime.Year和r.SomeDateTime.Month,按年份排序,然后按名称排序月份,分别选择年份和月份(连同计数),然后在需要时将字符串格式投影回客户端代码。顺便说一句,我怀疑您的第二个OrderBy应该是ThenBy-目前Name实际上是您订购的最重要的东西。以上就是C#学习教程:LinqtoSql–DateTimeformat–YYYY-MMM(2009年3月)分享的全部内容。如果对你有用,需要了解更多C#学习教程,希望大家多多关注——本文来自网络收藏,不代表立场,如涉及侵权,请右击联系管理员删除。如需转载请注明出处: