惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
V
V2EX
爱范儿
爱范儿
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
小众软件
小众软件
Vercel News
Vercel News
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
J
Java Code Geeks
P
Proofpoint News Feed
MongoDB | Blog
MongoDB | Blog
B
Blog
美团技术团队
量子位

博客园 - a-peng

My post test IE inline edit how to make hidden element actually hide EntityFramework4.1 Mapping Asp.Net Mvc 3.0 Windbg IOC与AOP Custom Date and Time Format Strings asp.net life cycle OH MY GOD! 我的后台页面都被IE可缓存了。。。 编译器错误消息: 类型“System.Data.Objects.DataClasses.EntityObject”在未被引用的程序集中定义。 让IFrame自适应高度 让IE8使用IE7兼容模式运行 - a-peng - 博客园 Visual Studio 2008 Html Designer无法加载 修改SQLServer2008数据表无法提交 jQuery plugin LazyForm定制您的CheckBox Radio和Select [转载] [翻译]在ASP.NET MVC中使用TDD与依赖注入 Asp.Net Mvc使用Ajax.BeginForm上传文件Request.Files始终为empty Asp.Net Mvc Ajax偏方 太疯狂了:Teleport Pro......It's incredible.
group by Year(CreatedDate), Month(CreatedDate) 用sql, ent...
a-peng · 2010-02-06 · via 博客园 - a-peng

Posted on 2010-02-06 16:44  a-peng  阅读(2583)  评论()    收藏  举报


博客左边的存档相信大家都很常见到吧。
我写了句sql语句,希望取出年份月份记录数。

select 
    
Year(CreatedDate) as Year,
    
Month(CreatedDate) as Month,
    
Count(PostId) as Count
from
    Post
group by
    
Year(CreatedDate),
    
Month(CreatedDate)
order by
    
Year descMonth desc


然后我希望在Entity Framework里实现,我用Entity SQL实现了下。

select 
    
Year,
    
Month,
    
Count(it.PostId) as Count
from
    Entities.Post 
as it
group by
    
Year(it.CreatedDate) as Year,
    
Month(it.CreatedDate) as Month
order by
    
Year descMonth desc


生成SQL语句

SELECT 
[Project1].[C4] AS [C1]
[Project1].[C1] AS [C2]
[Project1].[C2] AS [C3]
[Project1].[C3] AS [C4]
FROM ( SELECT 
    
[GroupBy1].[K1] AS [C1]
    
[GroupBy1].[K2] AS [C2]
    
[GroupBy1].[A1] AS [C3]
    
1 AS [C4]
    
FROM ( SELECT 
        
[Extent1].[K1] AS [K1]
        
[Extent1].[K2] AS [K2]
        
COUNT([Extent1].[A1]AS [A1]
        
FROM ( SELECT 
            
DATEPART (year[Extent1].[CreatedDate]AS [K1]
            
DATEPART (month[Extent1].[CreatedDate]AS [K2]
            
[Extent1].[PostId] AS [A1]
            
FROM [dbo].[aurora_Blogs_Post] AS [Extent1]
        )  
AS [Extent1]
        
GROUP BY [K1][K2]
    )  
AS [GroupBy1]
)  
AS [Project1]
ORDER BY [Project1].[C1] DESC[Project1].[C2] DESC


如果按上面的思路用linq表达式发现要group by两次,貌似很复杂。
换个算法。

代码

from p in posts
let m 
= new ArchiveDateTime { Year = p.CreatedDate.Year, Month = p.CreatedDate.Month }
group p by m into months
orderby months.Key descending
select 
new ArchiveData { Year = months.Key.Year, Month = months.Key.Month, PostCount = months.Count() };


Entity Framework不支持带参构造函数。所以你不能使用用let m = new DateTime(p.CreatedDate.Year, p.CreatedDate.Month, 1)

所以只能自己新建一个ArchiveDateTime。