我想编写一个Linq to Sql查询,它通过Username和DateTime进行计数和分组.我希望DateTime的格式如下“YYYY-MMM”(即2009年3月).我认为这会工作,但Linq to Sql无法解析ToString“format”参数.dc.MyTable.GroupBy(r =...

我想编写一个Linq to Sql查询,它通过Username和DateTime进行计数和分组.我希望DateTime的格式如下“YYYY-MMM”(即2009年3月).
我认为这会工作,但Linq to Sql无法解析ToString“format”参数.
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(…).
织梦狗教程
本文标题为:c# – Linq to Sql – 日期时间格式 – YYYY-MMM(2009年3月)


基础教程推荐
猜你喜欢
- 详解LINQ入门(下篇) 2023-02-03
- C#根据前台传入实体名称实现动态查询数据 2023-07-19
- c# 异步编程基础讲解 2023-04-09
- 利用C#实现AOP常见的几种方法详解 2022-11-14
- dotnet core 在CentOS下 使用libreoffice 把Office 转换成pdf 2023-09-26
- C#9.0 新特性简介 2023-03-13
- C#服务器NFS共享文件夹搭建与上传图片文件的实现 2023-06-22
- C# 实例化接口对象的方法 2022-11-03
- 在WPF中使用多线程更新UI 2023-06-20
- 使用C#实现Windows组和用户管理的示例代码 2023-03-28